Jump to content

Recommended Posts

I'm very new to PHP, so some of this may be very obvious to others, so I'm hoping for some help.....

I'm just looking for how to send in multiple variables and send one message to multiple email addresses. I get it to work if I only use one variable, but not multiple.

 

Thanks in advance!!!

 

(the error that I get back from my Charles debugger........ You must provide at least one recipient email address.<br />^8_2

 

 

 

 

require_once ('mail/class.phpmailer.php');

 

function sentEmailSMTP2($email, $email2)

{

 

 

$string2 = $email . "@yahoo.com" . "," . $email2 . "@yahoo.com" . "," . $email2 . "@hotmail.com";

 

$to = $string2;

 

$from = "me@domain.com";

$subject = 'messageX';

$message = 'Its ready';

$headers = 'From: me2@domain.com' . "\r\n" . 'Reply-To: me2@domain.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

 

 

$host = "mail.domain.com";

$username = "UID@domain.com";

$password = "pass";

$mail = new PHPMailer();

 

$mail -> IsSMTP();

// telling the class to use SMTP

$mail -> SMTPAuth = true;

$mail -> SMTPKeepAlive = true;

$mail -> Host = $host;

$mail -> Username = $username;

$mail -> Password = $password;

$mail -> SetFrom($from, 'Test ');

$mail -> Subject = $subject;

$mail -> AddAddress($string2);

$mail -> Body = $message;

if (!$mail -> Send())

{

echo "Mailer Error (" . str_replace("@", "@", $string2) . ') ' . $mail -> ErrorInfo . '<br />';

}

else

{

echo "Message sent to :" . $string2 . ' (' . str_replace("@", "@", $string2) . ')<br />';

}

 

}

Link to comment
https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/
Share on other sites

I don't see where you're passing your recipients ($to) to the mail handler.

 

Small, but annoying... why do this:

 

$string2 = $email . "@yahoo.com" . "," . $email2 . "@yahoo.com" . "," . $email2 . "@hotmail.com";
$to = $string2;

 

Just do:

 

$to = $email . "@yahoo.com" . "," . $email2 . "@yahoo.com" . "," . $email2 . "@hotmail.com";

 

EDIT: my bad, I see that you're passing $string2. Confusion with your choice in variable names.

Edited by mrMarcus

You can remove $to from the script or to be like @mrMarcus wrote - $to = $email . "@yahoo.com" . "," . $email2 . "@yahoo.com" . "," . $email2 . "@hotmail.com";

After that, just assign it to an array instead of a string.

 

$to = array($email . "@yahoo.com" . "," . $email2 . "@yahoo.com" . "," . $email2 . "@hotmail.com");

Just taking 3 seconds and looking at the documentation for phpmailer, it appears each recipient must be added separately:

 

$mail->AddAddress($email);
$mail->AddAddress($email2);

That' s why I told him to use array.

Repeating the same code multiple times is a very bad practice, too.

Edited by jazzman1

That' s why I told him to use array.

Repeating the same code multiple times is a very bad practice, too.

 

$mail->AddAddress() does not accept an array. It accepts 2 arguments/values:

 

$mail->AddAddress('john@doe.com', 'John Doe');

 

With the second argument being optional.

 

Best "practice" in a case where you might have several to many recipients might be to collect your recipients in an array, first, and then loop through the array. Keeping in mind that each individual recipient must be added to the mail handler individually.

$mail->AddAddress() does not accept an array. It accepts 2 arguments/values:

 

$mail->AddAddress('john@doe.com', 'John Doe');

 

With the second argument being optional.

 

Best "practice" in a case where you might have several to many recipients might be to collect your recipients in an array, first, and then loop through the array. Keeping in mind that each individual recipient must be added to the mail handler individually.

No, that doesn't make any sense!

You want to tell me that if I want to send 1000 mails, I need to copy/paste that property 1000 times?

It could be something like this, I preffer this format it's called - RFC # 822

Example:

 

$e_list = array("joeDoe@gmail.com"=>"Joe Doe","joeDoe_1@gmail.com"=>"Joe Doe 1","joeDoe_2@gmail.com"=>"Joe Doe 2","joeDoe_3@gmail.com"=>"Joe Doe 3");
$mail->AddAddress($e_list);

Ummm. Even ignoring the documentation for the method in question, here's the definition of the addaddress() method -

 

  public function AddAddress($address, $name = '') {
   return $this->AddAnAddress('to', $address, $name);
 }

 

and the start of the addanaddress method -

 


 private function AddAnAddress($kind, $address, $name = '') {
   if (!preg_match('/^(to|cc|bcc|ReplyTo)$/', $kind)) {
  echo 'Invalid recipient array: ' . kind;
  return false;
   }
   $address = trim($address);
   $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim

 

It/they DON'T accept an array of addresses.

No, that doesn't make any sense!

You want to tell me that if I want to send 1000 mails, I need to copy/paste that property 1000 times?

It could be something like this, I preffer this format it's called - RFC # 822

Example:

 

$e_list = array("joeDoe@gmail.com"=>"Joe Doe","joeDoe_1@gmail.com"=>"Joe Doe 1","joeDoe_2@gmail.com"=>"Joe Doe 2","joeDoe_3@gmail.com"=>"Joe Doe 3");
$mail->AddAddress($e_list);

 

The AddAddress() function does not accept arrays.

 

When you add a recipient to the AddAddress() function, it queues each recipient for bulk sending. No email is sent until x number of recipients have been added to the queue whether it be 1, 10, or 10,000.

 

Where did you get that:

$mail->AddAddress('john@doe.com', 'John Doe');

It should be:

$mail->AddAddress(array('john@doe.com', 'john@doe.com' => 'John Doe'));

 

No, it should be:

 

$mail->AddAddress('john@doe.com', 'John Doe');

 

If you don't believe me, try it your way and post your results.

Hm...@mrMarcus, you are right, that method doesn't accept an array only a string -> http://phpmailer.wor....php?pg=methods

I've never used phpMailer.

 

@OP, try this

 

$string2 = array(
$email.'@yahoo.com' => 'Person One',
$email2.'@yahoo.com' => 'Person Two',
$email2.'@hotmail.com' => 'Person Three'
// ...etc
);
foreach($string2 as $email => $name)
{
$mail->AddAddress($email, $name);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.