Php Mailer To Multiple Addresses Issue
#1
Posted 21 November 2012 - 08:34 PM
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 />';
}
}
#2
Posted 22 November 2012 - 09:41 AM
#3
Posted 22 November 2012 - 09:58 AM
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, 22 November 2012 - 10:08 AM.
#4
Posted 22 November 2012 - 10:08 AM
After that, just assign it to an array instead of a string.
$to = array($email . "@yahoo.com" . "," . $email2 . "@yahoo.com" . "," . $email2 . "@hotmail.com");
#5
Posted 22 November 2012 - 10:09 AM
#6
Posted 22 November 2012 - 10:12 AM
$mail->AddAddress($email); $mail->AddAddress($email2);
#7
Posted 22 November 2012 - 11:20 AM
That' s why I told him to use array.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);
Repeating the same code multiple times is a very bad practice, too.
Edited by jazzman1, 22 November 2012 - 11:21 AM.
#8
Posted 22 November 2012 - 11:35 AM
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.
#9
Posted 22 November 2012 - 12:07 PM
No, that doesn't make any sense!$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.
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);
#10
Posted 22 November 2012 - 12:19 PM
$mail->AddAddress('john@doe.com', 'John Doe');
It should be:
$mail->AddAddress(array('john@doe.com', 'john@doe.com' => 'John Doe'));
#11
Posted 22 November 2012 - 12:28 PM
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.
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.
Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.
#12
Posted 22 November 2012 - 12:32 PM
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.
#13
Posted 22 November 2012 - 01:10 PM
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);
}
#14
Posted 23 November 2012 - 02:42 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












