使用 phpmailer 发送感谢邮件
Send Thank you email with phpmailer
我有一个可以完美发送电子邮件的 phpmailer 表单,但是我想在每次提交表单时发送 2 封电子邮件。第一封电子邮件应该是我自己的表单字段值。这按预期工作。我很难找到任何文件,可以让我向提交表格的人发送第二封电子邮件,感谢您与我们联系,我们会尽快与您联系。我基本上想向您 $_POST['email']
发送第二封电子邮件,内容为 "Thank You"。
表单代码(无任何感谢邮件):
require 'library/extensions/PHPMailerAutoload.php';
$mail = new PHPMailer;
$strMessage = "";
//Only action if the form has been submitted
if(isset($_POST['submit-contact-new'])) {
//Validate the fields again, because not everyone has Javascript, including bots
if (isset($_POST['name']) && $_POST['name'] !== "" &&
isset($_POST['surname']) && $_POST['surname'] !== "" &&
isset($_POST['email']) && $_POST['email'] !== "" &&
isset($_POST['phone']) && $_POST['phone'] !== "" &&
isset($_POST['comment']) && $_POST['comment'] !== "") {
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply@domain.com'; // SMTP username
$mail->Password = 'passwd'; // SMTP password
$mail->From = 'noreply@domain.com';
$mail->FromName = 'Domain';
$mail->addAddress('staffmember@domain.com', 'First Last'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Domain Form Enquiry';
$mail->Body = '
<html>
<body>
<h1>Domain Website Enquiry</h1>
<p>Information on form was:</p>
<p><strong>Name</strong>: '.$_POST['name'].'</p>
<p><strong>Surname</strong>: '.$_POST['surname'].'</p>
<p><strong>Email</strong>: '.$_POST['email'].'</p>
<p><strong>Phone</strong>: '.$_POST['phone'].'</p>
<p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
</body>
</html>
';
if(!$mail->send()) {
//Finally redirect
header('Location: '.$_SERVER['REQUEST_URI']. '?message='.$strMessage) ;
} else {
//Finally redirect
header('Location: http://domain.com/thank-you?location='.$strLocation) ;
}
} else {
//Something is wrong, so work out what
if (!isset($_POST['name']) || $_POST['name'] === "") $strMessage .= "<li>Name must be entered</li>";
if (!isset($_POST['surname']) || $_POST['surname'] === "") $strMessage .= "<li>Surname must be entered</li>";
if (isset($_POST['surname']) && isset($_POST['surname']) && $_POST['name'] === $_POST['surname']) $strMessage .= "<li>Name and Surname must be different</li>";
if (!isset($_POST['phone']) || $_POST['phone'] === "") $strMessage .= "<li>Phone Number must be entered</li>";
if (!isset($_POST['email']) || $_POST['email'] === "") $strMessage .= "<li>Email must be entered</li>";
if (!isset($_POST['comment']) || $_POST['comment'] === "") $strMessage .= "<li>An enquiry must be entered</li>";
if ($strMessage !== "") $strMessage = "<ul>" . $strMessage . "</ul>";
//Finally redirect
header('Location: '.$_SERVER['REQUEST_URI']. '?message='.$strMessage) ;
exit();
}
}
?>
为了解决这个问题,我只是将另一个实例放入此表单操作中,然后 运行 phpmailer 两次。这使我能够使用相同的 post 内容设置单独的正文内容和收据。
<?php
require 'library/extensions/PHPMailerAutoload.php';
$mail = new PHPMailer;
$strMessage = "";
//Only action if the form has been submitted
if(isset($_POST['submit-contact-new'])) {
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply@domain.com'; // SMTP username
$mail->Password = 'passwd'; // SMTP password
$mail->From = 'noreply@domain.com';
$mail->FromName = 'Domain';
$mail->addAddress('staff@domain.com', 'Staff Name'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Domain Form Enquiry';
$mail->Body = '
<html>
<body>
<h1>Domain Website Enquiry</h1>
<p>Information on form was:</p>
<p><strong>Name</strong>: '.$_POST['name'].'</p>
<p><strong>Surname</strong>: '.$_POST['surname'].'</p>
<p><strong>Email</strong>: '.$_POST['email'].'</p>
<p><strong>Phone</strong>: '.$_POST['phone'].'</p>
<p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
</body>
</html>
';
if(!$mail->send()) {
//Finally redirect
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
//Finally redirect
header('Location: http://domain.com/thank-you?location='.$strLocation) ;
}
}
$mail = new PHPMailer;
$strMessage = "";
//Only action if the form has been submitted
if(isset($_POST['submit-contact-new'])) {
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply@rdomain.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->From = 'noreply@domain.com';
$mail->FromName = 'Domain';
$mail->addAddress(''.$_POST['email'].''); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->send()) {
//Finally redirect
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
//Finally redirect
header('Location: http://domain.com/thank-you?location='.$strLocation) ;
}
}
?>
我有一个可以完美发送电子邮件的 phpmailer 表单,但是我想在每次提交表单时发送 2 封电子邮件。第一封电子邮件应该是我自己的表单字段值。这按预期工作。我很难找到任何文件,可以让我向提交表格的人发送第二封电子邮件,感谢您与我们联系,我们会尽快与您联系。我基本上想向您 $_POST['email']
发送第二封电子邮件,内容为 "Thank You"。
表单代码(无任何感谢邮件):
require 'library/extensions/PHPMailerAutoload.php';
$mail = new PHPMailer;
$strMessage = "";
//Only action if the form has been submitted
if(isset($_POST['submit-contact-new'])) {
//Validate the fields again, because not everyone has Javascript, including bots
if (isset($_POST['name']) && $_POST['name'] !== "" &&
isset($_POST['surname']) && $_POST['surname'] !== "" &&
isset($_POST['email']) && $_POST['email'] !== "" &&
isset($_POST['phone']) && $_POST['phone'] !== "" &&
isset($_POST['comment']) && $_POST['comment'] !== "") {
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply@domain.com'; // SMTP username
$mail->Password = 'passwd'; // SMTP password
$mail->From = 'noreply@domain.com';
$mail->FromName = 'Domain';
$mail->addAddress('staffmember@domain.com', 'First Last'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Domain Form Enquiry';
$mail->Body = '
<html>
<body>
<h1>Domain Website Enquiry</h1>
<p>Information on form was:</p>
<p><strong>Name</strong>: '.$_POST['name'].'</p>
<p><strong>Surname</strong>: '.$_POST['surname'].'</p>
<p><strong>Email</strong>: '.$_POST['email'].'</p>
<p><strong>Phone</strong>: '.$_POST['phone'].'</p>
<p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
</body>
</html>
';
if(!$mail->send()) {
//Finally redirect
header('Location: '.$_SERVER['REQUEST_URI']. '?message='.$strMessage) ;
} else {
//Finally redirect
header('Location: http://domain.com/thank-you?location='.$strLocation) ;
}
} else {
//Something is wrong, so work out what
if (!isset($_POST['name']) || $_POST['name'] === "") $strMessage .= "<li>Name must be entered</li>";
if (!isset($_POST['surname']) || $_POST['surname'] === "") $strMessage .= "<li>Surname must be entered</li>";
if (isset($_POST['surname']) && isset($_POST['surname']) && $_POST['name'] === $_POST['surname']) $strMessage .= "<li>Name and Surname must be different</li>";
if (!isset($_POST['phone']) || $_POST['phone'] === "") $strMessage .= "<li>Phone Number must be entered</li>";
if (!isset($_POST['email']) || $_POST['email'] === "") $strMessage .= "<li>Email must be entered</li>";
if (!isset($_POST['comment']) || $_POST['comment'] === "") $strMessage .= "<li>An enquiry must be entered</li>";
if ($strMessage !== "") $strMessage = "<ul>" . $strMessage . "</ul>";
//Finally redirect
header('Location: '.$_SERVER['REQUEST_URI']. '?message='.$strMessage) ;
exit();
}
}
?>
为了解决这个问题,我只是将另一个实例放入此表单操作中,然后 运行 phpmailer 两次。这使我能够使用相同的 post 内容设置单独的正文内容和收据。
<?php
require 'library/extensions/PHPMailerAutoload.php';
$mail = new PHPMailer;
$strMessage = "";
//Only action if the form has been submitted
if(isset($_POST['submit-contact-new'])) {
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply@domain.com'; // SMTP username
$mail->Password = 'passwd'; // SMTP password
$mail->From = 'noreply@domain.com';
$mail->FromName = 'Domain';
$mail->addAddress('staff@domain.com', 'Staff Name'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Domain Form Enquiry';
$mail->Body = '
<html>
<body>
<h1>Domain Website Enquiry</h1>
<p>Information on form was:</p>
<p><strong>Name</strong>: '.$_POST['name'].'</p>
<p><strong>Surname</strong>: '.$_POST['surname'].'</p>
<p><strong>Email</strong>: '.$_POST['email'].'</p>
<p><strong>Phone</strong>: '.$_POST['phone'].'</p>
<p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
</body>
</html>
';
if(!$mail->send()) {
//Finally redirect
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
//Finally redirect
header('Location: http://domain.com/thank-you?location='.$strLocation) ;
}
}
$mail = new PHPMailer;
$strMessage = "";
//Only action if the form has been submitted
if(isset($_POST['submit-contact-new'])) {
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply@rdomain.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->From = 'noreply@domain.com';
$mail->FromName = 'Domain';
$mail->addAddress(''.$_POST['email'].''); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->send()) {
//Finally redirect
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
//Finally redirect
header('Location: http://domain.com/thank-you?location='.$strLocation) ;
}
}
?>