PHPMailer AddAddress()
You need to call the AddAddress
function once for each E-Mail address you want to send to. There are only two arguments for this function: recipient_email_address
and recipient_name
. The recipient name is optional and will not be used if not present.
$mailer->AddAddress('recipient1@example.com', 'First Name');
$mailer->AddAddress('recipient2@example.com', 'Second Name');
$mailer->AddAddress('recipient3@example.com', 'Third Name');
You could use an array to store the recipients and then use a for
loop.
How do I AddAddress automatically in PHPmailer from form php info?
Simple one... just change this line
$mail->AddAddress = ($_POST['email']);
to this:
$mail->AddAddress($_POST['email']);
as it is a method, as opposed to a variable, on the class! :)
there are some good examples you can use on the PHPMailer GitHub
PHP mailer multiple address
You need to call the AddAddress
method once for every recipient. Like so:
$mail->AddAddress('person1@domain.example', 'Person One');
$mail->AddAddress('person2@domain.example', 'Person Two');
// ..
Better yet, add them as Carbon Copy recipients.
$mail->AddCC('person1@domain.example', 'Person One');
$mail->AddCC('person2@domain.example', 'Person Two');
// ..
To make things easy, you should loop through an array to do this.
$recipients = array(
'person1@domain.example' => 'Person One',
'person2@domain.example' => 'Person Two',
// ..
);
foreach($recipients as $email => $name)
{
$mail->AddCC($email, $name);
}
Using array variable in $mailer- AddAddress(user@domain.com); PHPMailer script
You're closing the while
loop too soon.
Change it to:
require("PHPMailer_v5.1 2/class.phpmailer.php");
while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'ssl://smtp.gmail.com:465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = 'myemail@gmail.com'; // Sender's gmail address
$mailer->Password = 'the_password'; // Sender's gmail password
$mailer->From = "$from"; // Sender's email address
$mailer->FromName = "$from_name"; // senders name
$mailer->Body = "$msg";
$mailer->Subject = "$subject";
$mailer->AddAddress("$to"); // Recipient
if(!$mailer->Send())
{
echo 'Email sent to:' . $to . '<br/ >';
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo 'Email sent to:' . $to . '<br/ >';
}
// Then close your while loop here
}
mysqli_close($dbc);
PHPMailer sending to multiple address separated by a ,
You should explode it and then add the emails.
$addresses = explode(',', $result["emailto"]);
foreach ($addresses as $address) {
$email->AddAddress($address);
}
php auto mailer addAddress
I think this is a scope issue. If you are calling this code in the main body of the code
$mailadd=mysqli_query($db,"SELECT `doctor_email`
FROM `doctor_login`
WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
Then I assume you call mailit()
after this, so pass the data you want to use as a parameter to the mailit()
function so it is available inside the function scope.
function mailit($row)
{
$rand=rand(100000,999999);
$user=$_SESSION['user'];
$sql=mysqli_query($db,"UPDATE `doctor_login` SET `otp`='$rand' WHERE `doctor_id`='1'");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername '; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587 ; // TCP port to connect to
$mail->setFrom('dont_reply_back@example.xyz');
$mail->addAddress($row['doctor_email']); // Add a recipient
$mail->addReplyTo('dont_reply_back@example.xyz');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'THis is your OTP';
$mail->Body = 'Your otp is '.$rand;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
$mailadd=mysqli_query($db,"SELECT `doctor_email`
FROM `doctor_login`
WHERE `doctor_id`='1'");
$row = mysqli_fetch_array($mailadd,MYSQLI_ASSOC);
mailit($row);
You should have been getting errors from your original code so in future add the following:
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
// if you are using the `mysqli` API
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors.
How to use phpmailer to send email to multiple addresses one by one?
The problem you have is that you're actually adding multiple recipient addAddress()
before actually sending the email.
So, after your loop...
$mail->addAddress('a@a.com'); // Add a recipient
$mail->addAddress('b@c.com'); // Add a another recipient
$mail->addAddress('d@e.com'); // Add a another recipient
the TO
address is now a@a.com, b@c.com, d@e.com
And then... you're sending the email to all of them.
To send the email one by one
I would initialize the mail
object completely inside the loop.
(or call another function passing the address as an argument).
PHPMailer not sending all emails to last email address
Because you're reusing the $mail
object to addAddress()
and send()
. So the first time you call phpmaileremail()
the first address gets the email. Then when you call it for the second time the second address is added and the first and second address get the email. And so on.
A simple solution would be to create the $mail
object inside the phpmaileremail()
function:
function phpmaileremail($reciever,$usertype, $file, $emailsubject, $email_body )
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX@gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
PS: Not that it matters, but reciever
is written receiver
. I've made that mistake as well.
PHP Mailer Multiple recipients
You're code looks like it should be doing the trick. Make sure that $address
doesn't contain any whitespace for the entries. For safe measure, add the trim()
function.
$mail->AddAddress(trim($address));
If that does not work, make sure that you're recipient addresses are real.
Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC()
instead of AddAddress()
so that their addresses are not revealed.
Related Topics
Magento - Retrieve Products with a Specific Attribute Value
Best Way to Determine If a Url Is an Image in PHP
How to Get Ssl Certificate Info with Curl in PHP
Can You Add an If Statement in Order By
PHP Flush/Ob_Flush Not Working
Woocommerce: Assigning an Endpoint to a Custom Template in My Account Pages
Prevent Innodb Auto Increment on Duplicate Key
Symfony2 - Creating Own Vendor Bundle - Project and Git Strategy
How to Fix Stream_Socket_Enable_Crypto(): Ssl Operation Failed with Code 1
Header in PDF Page Using Dompdf in PHP
Check If PHP-Page Is Accessed from an iOS Device
Centos 7/Apache/PHP - Mkdir(): Permission Denied
How to Make Number_Format() Not to Round Numbers Up
Php: Multiple SQL Queries in One MySQL_Query Statement