如何添加 PHP 脚本以使用 "thank you" 消息自动回复

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

我是 php 新手,刚刚弄清楚如何使用 php 和 phpmailer 将我用户的电子邮件地址发送到我的电子邮件地址以添加到时事通讯.

但是,现在我想在 php 中添加一个简单的自动回复脚本,因此当用户将他们的电子邮件添加到我的访客列表时,它会向他们发送一封自动回复电子邮件,内容如下:

Thanks for signing up. [Picture of my logo] 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 user

那么我可以做些什么来解决这个问题,以便用户在收到自动回复时看到我的电子邮件地址,以及如何确保它说的是我的名字而不是 root 用户?

添加:

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();

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

使用 SMTPSecure = 'ssl'Port = 25 是一个非常不寻常的组合,而且很可能是错误的。 ssl/465 和 tls/587 比较常见。

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

您的代码似乎是基于旧示例(尝试 a new one) - make sure you are using latest PHPMailer - 至少 5.2.10。