Sendmail sending to multiple addresses but in seperate emails - php

I'm trying to send multiple separate emails to multiple addresses. The below code sends it in 1 email with multiple TO addresses. This becomes an issue because everyone in the email can see each other's email addresses.
Is there a way to send separate emails?
<?php
$smtp = 'xxx.com';
$port = 25;
$secure = 'tls';
$username = 'test#xxx.com';
$pass = '';
$from = 'test#xxx.com';
$to = 'info#xxx.com';
$to1 = '';
$subject = 'Test Email';
$content = $mail_content;
require_once("include/class.phpmailer.php");
$mail=new PHPMailer(true);
$mail->IsSMTP();
try{
$mail->Host = $smtp;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->SMTPSecure = $secure;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SetFrom($from);
if (isset($email) && $email) {
$mail->AddAddress($email);
}
else {
while($row = mysqli_fetch_object($result)) {
$mail->AddAddress($row->email);
echo $row->email."<br>";
}
}
$mail->Subject = $subject;
$mail->MsgHTML($content);
$mail->Send();
if (isset($email) && $email) {
?>
<script>location.href="<?php echo '../index.php' . $_REQUEST['redirect']; ?>";</script>
<?php
}
}
catch (phpmailerException $e){
echo $e->errorMessage();
}
catch (Exception $e){
echo $e->getMessage();
}
?>

Send the email in the BCC.
Look here for an example :
PHP Email sending BCC

You have a loop that checks for all of the email addresses and then assigns the email address to $mail->AddAddress().
All you need to do in this same loop is send the email with $mail->Send(); with each iteration of the loop as well
//PHPmailer object
$mail=new PHPMailer(true);
//set up
$mail->IsSMTP();
$mail->Host = $smtp;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->SMTPSecure = $secure;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SetFrom($from);
$mail->Subject = $subject;
$mail->MsgHTML($content);
//add address
if (isset($email) && $email) {
$mail->AddAddress($email);
}
else {
//loop through the DB results
while($row = mysqli_fetch_object($result)) {
//you already have this
$mail->AddAddress($row->email);
echo $row->email."<br>";
//now send it here as well
$mail->Send();
//Do more stuff here
}
}
I have not tested this on my local system but this is one way to send the mail individually for each address as an alternative to sending one mail and using bcc.

Related

Getting authentication error while sending mail with PHPmailer using PHP

I am getting the following error while sending the email using PHPMailer.
Error:
SMTP Error: Could not authenticate.
I am explaining my code below.
<?php
require_once('/var/www/oditek.in/subhra/phpmailer/class.phpmailer.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
I have also turned on the the less secured app option in gmail but still same error is coming. This is the implementation like contact us form in website. User will send the email request to admin(here tuurbus). Please help me to resolve this issue.
Download Phpmailer package from https://github.com/PHPMailer/PHPMailer/ link & copy into your project and extract zip folder into your project and change the your code as following also check your password ,email id. Its works by my side.
<?php
include_once('PHPMailerAutoload.php');
function SentMail($to,$from,$subject,$msg_body,$reply_to='',$cc='',$files=''){
$mail = new PHPMailer();
$body = $msg_body;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = "tuurbus#gmail.com";
$mail->Password = "abcd_bs#123";
$mail->SetFrom($from,'tuurbus');
if($reply_to!=''){
$mail->AddReplyTo($reply_to,'tuurbus');
}
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address);
if(count($files) > 0 && $files!=''){
for($i=0;$i<=count($files);$i++){
if(is_file($files[$i])){
$mail->AddAttachment($files[$i]);
}
}
}
if($cc!=''){
$addrcc = explode(',',$cc);
foreach ($addrcc as $addresscc) {
$mail->AddCC(trim($addresscc));
}
}
if($mail->Send()){
return 1;
}else{
return 0;
}
}
$to="tuurbus#gmail.com";
$from="subhrajyotipradhan#gmail.com";
$subject="Test email";
$msg_body="Hi, This is customize request";
$ret = SentMail($to,$from,$subject,$msg_body);
echo $ret;exit;
?>
Try:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

Sending multiple mails in php mail

I am trying to send the multiple mails through my mailer from the below code, But it is showing error
apache http error occurred.
<?php
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'Name');
$mail->addReplyTo('info#domain.com', 'Name');
// read the list of emails from the file.
$email_list = file("maillist1.csv");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list in
require 'phpmailer/PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.domain.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->setFrom('info#domain.com', 'Name');
$mail->addReplyTo('info#domain.com', 'Name');
// read the list of emails from the file.
$email_list = file("maillist1.csv");
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
// implode the list into a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
if ( mail($to,$subject,$message) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
}
?>to a single variable, put commas in, apply as $to value.
$to = implode(",",$email_list);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
if ( mail($to,$subject,$message) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
}
?>

How to send multiple emails by php variable in Addcc

From $list i am getting all the emails that are posted by form after using explode
$list = explode(',', $guest_email);
foreach ($list as $k => $v) {
$mail = new PHPmailer();
$mail->From = $smtp_user;
$mail->FromName = $fromname;
$mail->Mailer = "mail";
$mail->Subject = "";
$mail->IsHTML(true);
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = ;
$mail->SMTPSecure = '';
$mail->Host = $smtp;
$mail->Port = $port;
$mail->Password = $sm;
$mail->Username = $sm;
$mail->Body = $bodyContent;
in below line the mail are going to all posted email address, in next line i want to add all the emails in cc, but it is not working
$mail->AddAddress($v);
$mail->Addcc($v)
$mail->AddReplyTo($smtp_user);
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "SUCCESS";
}
}
how to send all email in $mail->Addcc($v)

PHPMailer not working consistently

I have a web app using PHP, MySQL, and PHPMailer to send emails. The issue I am having is PHPMailer will send some of the emails, but not always. I'm thinking maybe it is because different list of emails have data issues? Below is my code that loops through the email addresses and send some, but not all the emails. I have tried to catch errors, but I can't see any in the logs.
How can I debug what is going on, and why some don't send?
else if($current_version == 'PROD'){
date_default_timezone_set('America/New_York');
require 'PHPMailer-master/PHPMailerAutoload.php';
$pageURL = 'xxxxxx';
$subject = "xxxxx ";
$body = "xxxxxx \r\n";
$body .= "xxxxx \r\n \r\n";
$body_html = str_replace("\r\n","<br/>",$body);
try {
$mail = new PHPMailer();
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
//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';
$mail->SMTPAuth = true;
$mail->Username = "xxx#xxxx.com";
$mail->Password = "xxxx";
$mail->setFrom('xxxxx#xxxx.com', 'xxxx');
$mail->addReplyTo('xxxx#xxxx.com', 'xxxx');
$mail->Subject = $subject;
$mail->Body = $body_html;
$mail->AltBody = $body;
$sql_email_list = "SELECT email FROM tbl_email
WHERE distro_name='xxxxs'
AND is_active='Y' ";
$result_email_list = mysql_query($sql_email_list);
$num_email_list = mysql_num_rows($result_email_list);
$lost_email_list = mysql_result($result_email_list,0,'email');
$lost_email_array = explode(";",$lost_email_list);
foreach ($lost_email_array as $key => $val){
$mail->AddAddress($val);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
$sql_email_one = "SELECT table2.email
FROM table1
INNER JOIN table2 ON table1.cntct = table2.cntct_id
WHERE id='$id' ";
$result_email_one = mysql_query($sql_email_one);
$num_email_one = mysql_num_rows($result_email_one);
for($iiz=0;$iiz<$num_email_one;$iiz++){
$one_email = mysql_result($result_email_one,$iiz,'email');
$mail->AddAddress($one_email);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
echo '<hr>ERROR 001';
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
echo '<hr>ERROR 002';
}
}

Sending multiple email from same page phpmailer

i am trying to send email to user and to admin at the same time using phpmailer but only one email is sent.. either to user or to admin..
i have tried every possible way with no luck. please help
below is my code.
$html.="This is my message to user";
$bcc = "xxx#gmail.com";
$to= $clientemail;
$subject = "This is my subject;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
try {
$mail->From = $from;
$mail->FromName = $Name;
$mail->AddAddress(''.$to.'');
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
$mail->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
$message="Thank you for your request.";
$html="this is my second message to admin";
$bcc = "xxx#gmail.com";
$to= $admin;
$subject = "message to admin;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail2 = new PHPMailer();
$mail2->IsSMTP();
$mail2->Host = "$host"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = $username; // SMTP username
$mail2->Password = $password; // SMTP password
$mail2->Port = 587;
$mail2->SMTPSecure = 'tls';
try {
$mail2->From = $from;
$mail2->FromName = $from;
$mail2->AddAddress(''.$to.'');
$mail2->AddBCC('xxx#gmail.com');
$mail2->WordWrap = 50;
$mail2->IsHTML(true);
$mail2->Subject = $subject;
$mail2->Body = $html;
$mail2->AltBody = $html;
$mail2->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
I'm not sure if you want to send the same email or not to differents persons, but the first improvement to do is to factorize your code.
// here an example of a function to send the SAME email to differents persons
// $emails is an array
function sendMail($emails, $subjects, $msg) {
$mail = new PHPMailer();
$mail->IsSMTP();
try {
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = $from;
$mail->FromName = $Name;
foreach ($emails as $to) {
$mail->AddAddress($to);
}
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
if(!$mail->Send()) {
// log if needed
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
}
now you can use this function with differents messages or subjects... whatever you want!
EDIT :
to send different message to users and admins :
//settings for admin
$emails = array('admin#mail.com', 'admin2#mail.com'); // or just array('admin#mail.com');
$subjects = 'my sub';
$msg = 'my msg';
sendMail($emails, $subjects, $msg);
//settings for users
$emails = array('user#mail.com');
$subjects = 'different or not sub';
$msg = 'different or not msg';
sendMail($emails, $subjects, $msg);

Categories