如何將一封電子郵件發送到多個電子郵件地址?現在這個 index.php 頁面是在線托管的,它連接到一個 send.php 頁面,我想添加另一個電子郵件文本欄位,該欄位也用于向其中輸入的地址發送電子郵件。
目前,它會向在表單的電子郵件欄位中輸入的一個電子郵件地址發送一封電子郵件。
索引.php
<form id="contactus" method="post" action="send.php">
<input type="name" name="name" class="form-control" placeholder="Name" required>. <br/><br/>
<input type="email" name="email" class="form-control" placeholder="Email" required>. <br/><br/>
<input type="name" name="name2" class="form-control" placeholder="Player 2's Email Address" required><br/><br/>
<textarea name="message" class="form-control" placeholder="What Is Your Question?" required></textarea><br/><br/>
<input type="hidden" name="phone2">
<button type="submit" class="btn btn-success">Send Email</button>
<br><br>
</form>
發送.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}
if(empty($email)){
header("location: index.php?noemail");
exit();
}
if(empty($message)){
header("location: index.php?noemail");
exit();
}
/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/
//added line; 'honeypot'
if(empty($_POST['phone2'])){
//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 1;
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");
$mail->setFrom('#Email_Placeholder', 'Greeting');
// Add a recipient : used to also send to name
$mail->addAddress($email);
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
/*$mail->addBCC('bcc@example.com');*/
//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you?
<br>
<b> Please Notice: </b><br>
<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 .
"</p>";
$mail->isHTML(true);
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com");
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
uj5u.com熱心網友回復:
為了代碼中的可讀性,使用陣列并將其內爆為逗號分隔的字串:-
$recipients = array(
"[email protected]",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
uj5u.com熱心網友回復:
這很簡單——如果你按照代碼,你只需要復制一些行。
復制您的表單域
<input type="email" name="email" class="form-control" placeholder="Email" required>
<input type="email" name="email_2" class="form-control" placeholder="Email" required>
復制您的驗證
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);
復制“添加地址”
$mail->addAddress($email);
$mail->addAddress($email_2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352665.html
