Olana Kenea
Olana Kenea

Reputation: 39

how to send two different email to two different email addresses using PHPMailer in php

I am trying to send two different emails to two different recipients using PHPmailer but only the second email is arriving.

My code:

 /**
 * 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 = "[email protected]";

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

//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'Department of Information Science');

//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', '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!";
}

the second email is delivered successfully but the first one is not.

Upvotes: 1

Views: 2151

Answers (2)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

There are a couple of different possibilities. The fact that the second one is sending properly is a good indication that your code is working in general. Focusing on the first one, I'd suggest three things:

  1. Add error checking to the first send() call. You have if (!$mail->Send()) {... on the second one, but you aren't checking the first one. You can use $mail->ErrorInfo as you have in a comment in the second part. (By the way, the $mail->ErrorInfo you have in the script tag will not work. Variables in single quoted strings like that will not be parsed, so you'll just get the literal string "$mail->ErrorInfo" there if there is an error.)

  2. Add error checking to the first addAddress() call. PHPMailer will give you an error that you can check if the email address is invalid for some reason. As far as the code you've shown here, $email appears to be undefined, but so does $stud_email and you've said that one is working properly, so I assume those are both defined somewhere before the code that you've shown here, but a possible cause for this is that $email is undefined or doesn't have the value you expect it to.

  3. The email is being sent, but not received. It's pretty easy for a message to be mis-identified as spam at multiple points between the sender and the receiver. This is more difficult to diagnose, but if you add the error checking to the first send() call and don't get any errors, you'll at least be able to rule that out as a point of failure.

Upvotes: 2

Joshua Aranda
Joshua Aranda

Reputation: 1

you can do an array with de emails and subject.

$recipients = array(
   '[email protected]' => 'Person One',
   '[email protected]' => 'Person Two',
   // ..
);

Upvotes: 0

Related Questions