如何在php中使用phpMail将两封不同的电子邮件发送到两个不同的电子邮件地址

如何在php中使用phpMail将两封不同的电子邮件发送到两个不同的电子邮件地址,php,phpmailer,Php,Phpmailer,我正在尝试使用PHPmailer向两个不同的收件人发送两封不同的电子邮件,但只有第二封电子邮件到达 我的代码: /** * This code shows settings to use when sending via Google's Gmail servers. */ //SMTP needs accurate times, and the PHP time zone MUST be set //This should be done in your php.ini, but th

我正在尝试使用PHPmailer向两个不同的收件人发送两封不同的电子邮件,但只有第二封电子邮件到达

我的代码:

 /**
 * This code shows settings to use when sending via Google's Gmail servers.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require 'PHPMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "olaozias@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "password";

//Set who the message is to be sent from
$mail->setFrom('olaozias@gmail.com', 'Department of Information Science');

//Set an alternative reply-to address
$mail->addReplyTo('olaozias@gmail.com', 'Department of Information Science');

//Set who the message is to be sent to
$mail->addAddress($email , 'Parent');

//Set the subject line
$mail->Subject = 'Student Attendance System';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->Body = 'Dear Parent \r\n This email is sent from the university of gondar , Department of information science to inform you that your child '. $firstname.' has been registered for semester '.$semister. ' in order to see your child attendance status and to communicate easily with our department use our attendance system. First download and install the mobile application  which is attached in this email to your phone and use these login credentials to login to the system \r\n Your child Id: '.$student_no. '\r\n Password: '.$parent_pass.'\r\n Thank you for using our attendance system \r\n University of Gondar \r\n Department of Information Science ';

//Attach an image file
//$mail->addAttachment('AllCallRecorder.apk');
$mail->send();



$mail->ClearAddresses();

$mail->AddAddress($stud_email,'Student');
$mail->Subject = 'Student Attendance System';
$mail->Body = "email 2";


//send the message, check for errors
if (!$mail->Send()) {
    //echo "Mailer Error: " . $mail->ErrorInfo;
    echo '
                <script type = "text/javascript">
                    alert("Mailer Error: " . $mail->ErrorInfo);
                    window.location = "student.php";
                </script>
            ';
} else {
echo '
                <script type = "text/javascript">
                    alert("student Added successfully and an Email the  sent to email address provided");
                    window.location = "student.php";
                </script>
            ';
    //echo "Message sent!";
}
/**
*此代码显示通过谷歌Gmail服务器发送邮件时要使用的设置。
*/
//SMTP需要准确的时间,并且必须设置PHP时区
//这应该在您的php.ini中完成,但如果您没有访问权限,就应该这样做
日期\默认\时区\设置('Etc/UTC');
需要“PHPMailer/phpmailerautoad.php”;
//创建新的PHPMailer实例
$mail=新的PHPMailer;
//告诉PHPMailer使用SMTP
$mail->isSMTP();
//启用SMTP调试
//0=关闭(用于生产使用)
//1=客户端消息
//2=客户端和服务器消息
$mail->SMTPDebug=2;
//请求HTML友好的调试输出
$mail->Debugoutput='html';
//设置邮件服务器的主机名
$mail->Host='smtp.gmail.com';
//使用
//$mail->Host=gethostbyname('smtp.gmail.com');
//如果您的网络不支持IPv6上的SMTP
//为经过身份验证的TLS(又称RFC4409 SMTP提交)设置SMTP端口号-587
$mail->Port=587;
//将加密系统设置为使用-ssl(已弃用)或tls
$mail->SMTPSecure='tls';
//是否使用SMTP身份验证
$mail->SMTPAuth=true;
//用于SMTP身份验证的用户名-使用gmail的完整电子邮件地址
$mail->Username=”olaozias@gmail.com";
//用于SMTP身份验证的密码
$mail->Password=“Password”;
//设置要从谁发送消息
$mail->setFrom('olaozias@gmail.com","信息科学系",;
//将替代答复设置为地址
$mail->addReplyTo('olaozias@gmail.com","信息科学系",;
//设置要向谁发送邮件
$mail->addAddress($email,'Parent');
//设置主题行
$mail->Subject='学生考勤系统';
//从外部文件读取HTML消息正文,将引用的图像转换为嵌入的,
//将HTML转换为基本的纯文本替换体
//$mail->msgHTML(文件获取内容('contents.html')、目录名('uuuuuuuuu文件\));
//将纯文本正文替换为手动创建的正文
$mail ->身体=亲爱的父母R\n这封电子邮件是从贡达尔大学,信息科学系发送给你,告诉你的孩子。名字。'已注册学期'$semister'为了查看您孩子的出勤情况并方便与我们部门沟通,请使用我们的出勤系统。首先下载并安装本电子邮件中附带的移动应用程序,并使用这些登录凭据登录到系统\r\n您的孩子ID:‘$QuasixNO’\ r\n密码:‘$PARTENPASP.\\R\N感谢您使用我们的考勤系统\R\N大学的Gorda\R\N信息科学系’;
//附加图像文件
//$mail->addAttachment('AllCallRecorder.apk');
$mail->send();
$mail->ClearAddresses();
$mail->AddAddress($stud_email,'Student');
$mail->Subject='学生考勤系统';
$mail->Body=“email 2”;
//发送消息,检查错误
如果(!$mail->Send()){
//回显“邮件错误:”.$mail->ErrorInfo;
回声'
警报(“邮件错误:“.$mail->ErrorInfo”);
window.location=“student.php”;
';
}否则{
回声'
警报(“学生添加成功,并向提供的电子邮件地址发送电子邮件”);
window.location=“student.php”;
';
//回显“已发送消息!”;
}

第二封电子邮件已成功发送,但第一封电子邮件未成功发送。

您可以使用反电子邮件和主题创建一个数组

$recipients = array(
   'person1@domain.com' => 'Person One',
   'person2@domain.com' => 'Person Two',
   // ..
);

有两种不同的可能性。第二个正确发送的事实很好地表明您的代码在正常工作。针对第一个问题,我建议三件事:

  • 将错误检查添加到第一个
    send()
    调用。您在第二个部分有
    if(!$mail->Send()){…
    ,但您没有检查第一个。您可以使用
    $mail->ErrorInfo
    ,就像您在第二部分的注释中所做的那样。(顺便说一句,脚本标记中的
    $mail->ErrorInfo
    将不起作用。这样的单引号字符串中的变量将不会被解析,因此,如果出现错误,您只需在那里获得文本字符串“$mail->ErrorInfo”)

  • 将错误检查添加到第一个
    addAddress()
    call.PHPMailer将给您一个错误,您可以检查电子邮件地址是否因某种原因无效。就您在此处显示的代码而言,
    $email
    似乎未定义,但
    $stud\u email
    也未定义,并且您已经说过其中一个工作正常,因此我假设它们都是在您使用的代码之前定义的您已经在这里显示了,但可能的原因是,
    $email
    未定义或没有您期望的值

  • 电子邮件正在发送,但未收到。在发送者和接收者之间的多个点上,邮件很容易被错误识别为垃圾邮件。这更难诊断,但如果将错误检查添加到第一个
    send()
    如果您没有收到任何错误,您至少可以将其排除在故障点之外


  • 他想要不同的电子邮件您可以使用loop发送两封电子邮件?检查第一个电子邮件帐户的垃圾邮件文件夹我真心希望您不是在编写一个新的应用程序,该应用程序以纯文本形式向人们发送密码,这表明您也在以纯文本形式存储密码。请阅读PHP文档中的
    password\u hash
    。还可以设置
    $mail->SMTPKeepAlive=true
    ;它将在一个SMTP会话中发送两条消息,这将要快得多。这一点很好,@Synchro。另外,密码方面的问题也很好。老实说,我还没有真正查看电子邮件正文。