如何使用php

时间:2018-05-14 19:21:13

标签: 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 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!";
}

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

2 个答案:

答案 0 :(得分:2)

有几种不同的可能性。第二个正确发送的事实很好地表明您的代码一般都在工作。关注第一个,我建议三件事:

  1. 将错误检查添加到第一个send()来电。你在第二个上有if (!$mail->Send()) {...,但你没有检查第一个。您可以在第二部分的评论中使用$mail->ErrorInfo。 (顺便说一下,脚本标签中的$mail->ErrorInfo将不起作用。单引号字符串中的变量将不会被解析,因此您只需获取文字字符串&#34; $ mail - &gt; ErrorInfo&#34;如果有错误。)

  2. 将错误检查添加到第一个addAddress()来电。 PHPMailer会给您一个错误,您可以检查电子邮件地址是否因某种原因无效。至于您在此处显示的代码,$email似乎未定义,但$stud_email也是如此,并且您已经说过一个正常工作,所以我认为这些都是在您在此处显示的代码之前的某处定义,但可能的原因是$email未定义或没有您期望的值。

  3. 正在发送电子邮件,但未收到。在发件人和收件人之间的多个点上将邮件错误地识别为垃圾邮件非常容易。这更难以诊断,但如果您将错误检查添加到第一个send()来电并且没有收到任何错误,那么您至少可以将此作为故障。

答案 1 :(得分:0)

你可以用电子邮件和主题做一个数组。

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