Jump to content


Photo

Php Mailer To Multiple Addresses Issue


  • Please log in to reply
13 replies to this topic

#1 phpjayx

phpjayx

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 21 November 2012 - 08:34 PM

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("@", "&#64;", $string2) . ') ' . $mail -> ErrorInfo . '<br />';
}
else
{
echo "Message sent to :" . $string2 . ' (' . str_replace("@", "&#64;", $string2) . ')<br />';
}

}

#2 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,654 posts
  • LocationMississauga, Canada

Posted 22 November 2012 - 09:41 AM

Have you tried to change $string2 form a string to an array?

#3 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 22 November 2012 - 09:58 AM

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, 22 November 2012 - 10:08 AM.


#4 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,654 posts
  • LocationMississauga, Canada

Posted 22 November 2012 - 10:08 AM

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");


#5 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 22 November 2012 - 10:09 AM

If you echo $string2, do you get expected results? Is $string2 formatted properly as per how $mail->AddAddress() is expecting it?

#6 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 22 November 2012 - 10:12 AM

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);


#7 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,654 posts
  • LocationMississauga, Canada

Posted 22 November 2012 - 11:20 AM

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, 22 November 2012 - 11:21 AM.


#8 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

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 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,654 posts
  • LocationMississauga, Canada

Posted 22 November 2012 - 12:07 PM

$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);


#10 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,654 posts
  • LocationMississauga, Canada

Posted 22 November 2012 - 12:19 PM

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'));


#11 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 22 November 2012 - 12:28 PM

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.
Signature: (not a comment about anything you posted unless specifically indicated)
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 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

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 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,654 posts
  • LocationMississauga, Canada

Posted 22 November 2012 - 01:10 PM

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);
}


#14 phpjayx

phpjayx

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 23 November 2012 - 02:42 PM

Thank you all for the help, the last few posts got it working for me.... Thanks!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com