[英]php mailer add confirmation email to inputted address and form submit
我真的想保留這兩個功能——填寫表單字段> 表單提交> 轉到帶有信息的網站管理員電子郵件地址。
我沒有添加第二個功能; 我希望在表單提交后向用戶發送自定義確認消息(所有用戶都收到相同的默認消息) ——此消息通過電子郵件發送到用戶在“電子郵件”字段中插入的地址。 我得到了這個; 但是這樣做,我殺死了我原來的功能; 我想同時保持兩者; 到目前為止,我可以讓一個工作,但隨后另一個功能消失了——目前它只是向下面的兩封電子郵件發送確認。
<?php
/* config start website@website.com*/
$emailAddress = 'website@gmail.com'; // form goes here
$to = $_POST['email']; // user gets confirmation
$message = 'Thank you for submitting the form on our website.!';
/* config end */
require "phpmailer/class.phpmailer.php";
session_name("fancyform");
session_start();
foreach($_POST as $k=>$v)
{
if(ini_get('magic_quotes_gpc'))
$_POST[$k]=stripslashes($_POST[$k]);
$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}
$err = array();
if(!checkLen('name'))
$err[]='The name field is too short or empty!';
if(!checkLen('email'))
$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
$err[]='Your email is not valid!';
/* if(!checkLen('phone'))
$err[]='The phone field is too short or empty!'; */
/*if(!checkLen('subject'))
$err[]='You have not selected a subject!';*/
/* if(!checkLen('message'))
$err[]='The message field is too short or empty!';*/
if((int)$_POST['captcha'] != $_SESSION['expect'])
$err[]='The captcha code is wrong!';
if(count($err))
{
if($_POST['ajax'])
{
echo '-1';
}
else if($_SERVER['HTTP_REFERER'])
{
$_SESSION['errStr'] = implode('<br />',$err);
$_SESSION['post']=$_POST;
header('Location: '.$_SERVER['HTTP_REFERER']);
}
exit;
}
$msg=
'Name: '.$_POST['name'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Referred By: '.$_POST['referer'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->addAddress($to); // why can't both coexist?
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
$mail->MsgHTML($msg);
$mail->msgHtml($message); // why can't both coexist?
$mail->Send();
// $to = $_POST['email'];
// $message = 'Thank you for submitting the form on our website.!';
unset($_SESSION['post']);
if($_POST['ajax'])
{
echo '1';
}
else
{
$_SESSION['sent']=1;
if($_SERVER['HTTP_REFERER'])
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
}
function checkLen($str,$len=2)
{
return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}
function checkEmail($str)
{
return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
?>
我添加的代碼(有效,但覆蓋基本表單提交)
獲取在在線表單中的電子郵件字段中輸入的電子郵件 > 提交后在那里發送確認。
如果我把下面的幾行去掉; 我的表格工作正常; 用戶填寫表單,發送到指定的默認電子郵件地址 - 我只想添加確認消息,並保持表單正常工作。
$to = $_POST['email']; // user gets confirmation
$message = 'Thank you for submitting the form on our website.!';
$mail->addAddress($to); // why can't both coexist?
$mail->msgHtml($message); // why can't both coexist?
也許問題是如何允許多個$mail->MsgHTML
發送?
原始代碼; 我正在嘗試將第二個功能添加到:
<?php
/* config start website@website.com*/
$emailAddress = 'website@gmail.com';
/* config end */
require "phpmailer/class.phpmailer.php";
session_name("fancyform");
session_start();
foreach($_POST as $k=>$v)
{
if(ini_get('magic_quotes_gpc'))
$_POST[$k]=stripslashes($_POST[$k]);
$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}
$err = array();
if(!checkLen('name'))
$err[]='The name field is too short or empty!';
if(!checkLen('email'))
$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
$err[]='Your email is not valid!';
/* if(!checkLen('phone'))
$err[]='The phone field is too short or empty!'; */
/*if(!checkLen('subject'))
$err[]='You have not selected a subject!';*/
/* if(!checkLen('message'))
$err[]='The message field is too short or empty!';*/
if((int)$_POST['captcha'] != $_SESSION['expect'])
$err[]='The captcha code is wrong!';
if(count($err))
{
if($_POST['ajax'])
{
echo '-1';
}
else if($_SERVER['HTTP_REFERER'])
{
$_SESSION['errStr'] = implode('<br />',$err);
$_SESSION['post']=$_POST;
header('Location: '.$_SERVER['HTTP_REFERER']);
}
exit;
}
$msg=
'Name: '.$_POST['name'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Referred By: '.$_POST['referer'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
$mail->MsgHTML($msg);
$mail->Send();
unset($_SESSION['post']);
if($_POST['ajax'])
{
echo '1';
}
else
{
$_SESSION['sent']=1;
if($_SERVER['HTTP_REFERER'])
header('Location: '.$_SERVER['HTTP_REFERER']);
exit;
}
function checkLen($str,$len=2)
{
return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}
function checkEmail($str)
{
return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
?>
你不能用同一個實例發送兩封郵件——你需要將 $mail 重新分配給 PHPMailer 類的一個新實例:
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
$mail->MsgHtml($message);
$mail->Send();
///////////////////////////////////////////////////////////////
// then create new instance..
///////////////////////////////////////////////////////////////
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($to);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new Bridgetower.com Lead".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | Via your contact form feedback";
$mail->MsgHtml($message); // why can't both coexist?
$mail->Send();
您可以使用此代碼使用同一實例發送多封電子郵件。 (來自 Amazon SES 的修改代碼。)
$mail = new PHPMailer(true);
try {
// Specify the SMTP settings.
$mail->isSMTP();
$mail->setFrom($sender, $senderName);
$mail->Username = $usernameSmtp;
$mail->Password = $passwordSmtp;
$mail->Host = $host;
$mail->Port = $port;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->addAddress($recipient); // Original content added here
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $bodyHtml;
$mail->AltBody = $bodyText;
$mail->Send(); // The content of the form sent here
echo "Order Submitted: ";
$mail->ClearAddresses(); // Clear addresses
$mail->addAddress($customerEmail); // Email address entered on form
$mail->isHTML(true);
$mail->Subject = $confirmationSubject; // Custom subject
$mail->Body = $confirmationBodyHtml; // Custom reply
$mail->AltBody = $confirmationBodyText; // Text only custom reply
$mail->Send(); // Send confirmation email
echo "Confirmation Sent";
//echo "Email sent!" , PHP_EOL;
} catch (phpmailerException $e) {
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.