如何将PHP脚本添加到Auto response中;谢谢你";消息


How can I add a PHP script to Auto-respond with a "thank you" message

我是php新手,刚刚学会了如何将php与phpmailer一起使用,将用户的电子邮件地址发送到我的电子邮件地址,以便添加到时事通讯中。

但是,现在我想在php中添加一个简单的自动回复脚本,所以当用户将他们的电子邮件添加到我的留言列表时,它会向他们的电子邮件发送一封自动回复电子邮件,上面写着:

感谢您的注册。[我的标志图片]www.mysite.com

我搜索了又搜索,但我还没能找到关于如何在php中创建自动回复脚本的正确答案。请告诉我怎样才能完成这项任务。非常感谢。

<?php
$email = $_REQUEST['email'] ;
require("C:/inetpub/mysite.com/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
$mail->Host = "smtp.comcast.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myusername@comcast.net"; // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;                                    

$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("myemail@gmail.com", "Guestlist");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "A new member wishes to be added";
$message = $_REQUEST['message'] ;
$mail->Body = $email;
$mail->AltBody = $email;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
$mail2 = new PHPMailer();
// set mailer to use SMTP
$mail2->IsSMTP();
$mail2->Host = "smtp.comcast.net"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = "myusername@comcast.net"; // SMTP username
$mail2->Password = "*******"; // SMTP password
$mail2->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail2->Port = 25;                                    

$mail2->From = $email;
// below we want to set the email address we will be sending our email to.
$mail2->AddAddress("$email");
// set word wrap to 50 characters
$mail2->WordWrap = 50;
// set email format to HTML
$mail2->IsHTML(true);
$mail2->Subject = "Thank you for joining";
$message = "Please stay tune for updates" ;
$message = $_REQUEST['message'] ;
$mail2->Body = $message;
$mail2->AltBody = $message;
if(!$mail2->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail2->ErrorInfo;
exit;
}
echo "Message has been sent";
?>

更新:1好的,我学会了如何向用户发送自动回复电子邮件。然而,现在用户收到的消息带有他们自己的电子邮件地址和名称根用户

那么,我该怎么解决这个问题,让用户在收到自动回复时看到我的电子邮件地址,我该如何确保它显示的是我的姓名而不是root用户

不要使用提交者的电子邮件地址作为发件人地址-它不会起作用,因为它看起来像伪造的,并且会通过SPF检查。将您自己的地址作为发件人地址,并添加提交者的地址作为对的答复。

使用SMTPSecure = 'ssl'Port = 25是一种极不寻常的组合,很可能是错误的。ssl/465和tls/587更为常见。

要发送多条消息,不需要创建第二个实例,只需重复使用同一个实例即可。您可以重置所需的任何单个属性(如Body),并使用$mail->clearAddresses()清除地址,然后只需再次调用$mail->send()即可。

看起来你的代码是基于一个旧的例子(尝试一个新的例子)-确保你使用的是最新的PHPMailer-至少5.2.10。

添加:

echo "Message has been sent";
$mail = new PHPMailer();
$mail->From = 'myemail@gmail.com';
// below we want to set the email address we will be sending our email to.
$mail->AddAddress($email);
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "Thanks for joining ...";
$message = "Thanks for joining...";
$mail->Body = $email;
$mail->AltBody = $email;
$mail->Send();