phpjayx Posted November 22, 2012 Share Posted November 22, 2012 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 />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/ Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 Have you tried to change $string2 form a string to an array? Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394409 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 (edited) 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 November 22, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394412 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 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"); Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394416 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 If you echo $string2, do you get expected results? Is $string2 formatted properly as per how $mail->AddAddress() is expecting it? Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394417 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394418 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 (edited) 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 November 22, 2012 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394453 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394460 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 $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); Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394472 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 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')); Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394478 Share on other sites More sharing options...
PFMaBiSmAd Posted November 22, 2012 Share Posted November 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394481 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394482 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394494 Share on other sites More sharing options...
phpjayx Posted November 23, 2012 Author Share Posted November 23, 2012 Thank you all for the help, the last few posts got it working for me.... Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/271012-php-mailer-to-multiple-addresses-issue/#findComment-1394665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.