Send more then one e-mail in phpmailer

Pedro Chagas
Pedro Chagas used Ask the Experts™
on
Hi E's, I use phpmailer 5.0.2 for send e-mails. I use my gmail account (gmail smtp).
I use the basic code that comes with phpmailer script, and you can see them in snippet code. That script work very well, but just send for one box.
If I want send for more boxs, what I have to change in the code?
I think I have to change something in this lines:
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

Is possible send the same e-mail to various box's, and how?

Regards, JC
<?php
 
//error_reporting(E_ALL);
error_reporting(E_STRICT);
 
date_default_timezone_set('America/Toronto');
 
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
 
$mail             = new PHPMailer();
 
$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);
 
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password
 
$mail->SetFrom('name@yourdomain.com', 'First Last');
 
$mail->AddReplyTo("name@yourdomain.com","First Last");
 
$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
 
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
 
$mail->MsgHTML($body);
 
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
 
$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
 
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There are two ways that I would suggest to do this.  If this is an email blast to friends and family and you don't mind everyone seeing all the emails I suggest section 1 otherwise to maintain privacy you can do section 2:

One section you missed is the setting of the html body.
$mail->IsHTML(true);
You should add this to make sure that the body gets processed correctly.
<?php
// Section 1
$mail->AddAddress('recipient1@domain.com', 'First Person');
$mail->AddAddress('recipient2@domain.com', 'Second Person');
$mail->AddAddress('recipient3@domain.com', 'Third Person');
//OR If you have the names in an array:
foreach($array as $data) {
    $mail->AddAddress($data['email'], $data['name']);
}
 
// Section 2
foreach($array as $data) {
    // Send the emails in this loop.
    $mail->AddAddress($data['email'], $data['name']);
    $mail->Send();
    // DON'T FORGET TO ADD THIS OTHERWISE ALL NAMES AND ATTACHMENTS WILL BUILD UP
    $mail->ClearAddresses();
    $mail->ClearAttachments();
    // END DON'T FORGET
    $mail->IsHTML(false);
    echo "Mail sent to: $data['name']<br />";
}
?>

Open in new window

Expert of the Year 2008
Top Expert 2008
Commented:
Not sure what you mean by more boxes, but these two:
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

can be combined into a single line:
$mail->AddAddress("whoto@otherdomain.com", "John Doe");

Clearly, the way to add an address is with AddAddress(). So if you want to email "johnny" and "sally" as wellthen use:
$mail->AddAddress("whoto@otherdomain.com", "John Doe");
$mail->AddAddress("whoto2@otherdomain.com", "Johnny");
$mail->AddAddress("whoto3@otherdomain.com", "Sally");


Top Expert 2008
Commented:
Yes, you can send the same message to multiple recipients by using the $mail->AddAddress() method. If you want to prevent the recipients from seeing the name/email of the other recipients, use $mail->ClearAddresses(), like this:

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  # ok, send to other recipient:
  $mail->ClearAddresses();   # remove previous recipient
  $mail->AddAddress($address2, $name2);
  if(!$mail->Send()) {
    echo "Mailer Error (2. recipient): " . $mail->ErrorInfo;
  } else {
    echo "Message sent!";
  }
}

Open in new window

Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Pedro ChagasWebmaster

Author

Commented:
Great 3 solutions.
I just have a doubt, client see the e-mail I use for send the e-mails, and I want put "noreply@mydomain.com", like you can see in attach image.
What I have to change?

Regards, JC
mail1.jpg
Expert of the Year 2008
Top Expert 2008

Commented:
change:

$mail->SetFrom('name@yourdomain.com', 'First Last');


to:

$mail->SetFrom('noreply@mydomain.com');

and get rid of:
$mail->AddReplyTo("name@yourdomain.com","First Last");
Top Expert 2008

Commented:
I don't know if google/gmail will accept it, but try to change the From header, like this:

$mail->SetFrom('noreply@mydomain.com', 'DomainOrCompanyName');
Pedro ChagasWebmaster

Author

Commented:
I use:
$mail->SetFrom('noreply@mydomain.com', 'Test');
and appear the same thing!

Do you have any idea if it is possible?
Is better I open a new question for this issue?
Do you thing if I change phpmailer version I resolve the issue?

Regards, JC
Top Expert 2008

Commented:
Did you remove $mail->AddReplyTo() ?

>> Do you have any idea if it is possible?

It is possible, but I don't know if it is possible with Gmail.

>> Is better I open a new question for this issue?

It is a different question, so you should open a new question, yes... :)

Doing so might attract the attention of more experts.

>> Do you thing if I change phpmailer version I resolve the issue?

I don't think the version of phpmailer is relevant.
I was in the middle of writing a response when I saw cxr's answer come across so I will remove all that he stated above.  I don't believe it is since most of the major carriers (Yahoo, MSN, etc) use an MUA that is not controlled by the user but rather their own system of authenticated user.  This translates to the fact that the user can't send spam from their account (which is what you are trying to do sort of, although this is not your intention it is seen like that through the mail system).
Pedro ChagasWebmaster

Author

Commented:
Hold on @termlimit, I'm not a spammer, very far of this.....
I search a new solution for my project "allfreephoto.com". The project consist in customer made a download of pictures, but this download is made by email (customer choose the picture and custumer receive that picture in e-mail).
In my recent tests I discovery some of that e-mails go to the junk box of customers mail recipients, and others don't enter in her recipients. So, I think in phpmailer with gmail smtp! (In all of time I use the smtp of allfreephoto)
(All versions of allfreephoto send about 100 - 120 pictures everyday by e-mail)

Because I don't want customer reply emails (have a place in the site for customer contact us), I have the idea of put the e-mail sender "noreply@allfreephoto.com".
Is just this.....

Regards, JC
joaochagas I caveat that by saying that this was not your intention I did not mean to offend. The way that the MUA perceives this action.  When using a Gmail account (or any account for that matter) they typically require you to login to verify who you are.  If you change your from address and send an email it defeats the purpose of the authentication in the first place.  It is the same for a Spam message that you receive from a bank saying that you need to supply your login and password to verify who you are.  People changing their from address is a possible case of spam (again as seen from the mail administrators point of view even if this is not your intention as it is NOT in your case).

My suggestion would be to see if your hosting provider gives you email addresses along with your hosting plan.  If they do you can create the email "no-reply@allfreephoto.com", but shut the account off for receiving emails.  This way you can send emails through the account and the relevant header information is correct and the likelihood of a message getting placed into junk folders is minimized.

Again apologies if my explanation came across as an accusation.  I in no way think that is your intention, I was just trying to give you the network protocol's perceived view of what you are doing.
Pedro ChagasWebmaster

Author

Commented:
I'm sorry too, thanks for all!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial