Multiple Recipients with mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Multiple Recipients with mail()

Post by AnsonM »

Code: Select all

<?php
/* recipients */
$to  = "email1@hotmail.com" . ", ", ", ", ", " ; // note the comma
$to .= "email2@hotmail.com";
$to .= "email3@hotmail.com";
$to .= "email4@hotmail.com";
$to .= "email5@hotmail.com";
$to .= "email6@hotmail.com";

/* subject */
$subject = ":: SHUT UP EMAIL ::";

/* message */
$message = '
<html>
<body>
<p>This Email Has Been Sent From 8th-Dimension . Net from people that hate you.</p>
</body>
</html>
';

/* To send HTML mail, you can set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: email <email@hotmail.com\r\n";
$headers .= "From: Mail() PHP Script <rubbish@8th-dimension.net>\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>
<font color="FF0000">Thank You! Your Email has been sent 5 times to emails@hotmail.com</font>

Theres something wrong with:

Code: Select all

<?php
$to  = "email1@hotmail.com" . ", ", ", ", ", " ; // note the comma
?>
...

Whats wrong with the commas?[/php_man]
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

Code: Select all

<?php 
$to  = "email1@hotmail.com" . ", " . ", " . ", " . ", " . ", " ; // note the comma 
?>
EDIT: hmmmm.... still doesnt work.. but it doesnt show any parse error..

i think it does send... but not to all the 6 emails...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The output of:

Code: Select all

$to  = "email1@hotmail.com" . ", " . ", " . ", " . ", " . ", " ;
$to .= "email2@hotmail.com";
$to .= "email3@hotmail.com";
$to .= "email4@hotmail.com";
$to .= "email5@hotmail.com";
$to .= "email6@hotmail.com";

echo $to;
is

Code: Select all

email1@hotmail.com, , , , , email2@hotmail.comemail3@hotmail.comemail4@hotmail.comemail5@hotmail.comemail6@hotmail.com
Maybe this is more what you are looking for:

Code: Select all

$to = array();
$to[] = "email1@hotmail.com";
$to[] = "email2@hotmail.com";
$to[] = "email3@hotmail.com";
$to[] = "email4@hotmail.com";
$to[] = "email5@hotmail.com";
$to[] = "email6@hotmail.com";

$to = implode('; ', $to);

echo $to;

Code: Select all

email1@hotmail.com; email2@hotmail.com; email3@hotmail.com; email4@hotmail.com; email5@hotmail.com; email6@hotmail.com
Mac
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

its just meant to send it to every one of those emails each time the script is executed.. thats all...

its just weird how it doesnt send to all the 6 emails listed there..
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

Code: Select all

$to = array(); 
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";

$to = implode('; ', $to);
Doesn't even send 1 email..
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

I think it was supposed to be more of:

Code: Select all

$to = implode(",", $to);
-Nay
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

lol, still nothing.. this is odd..
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

AnsonM wrote:

Code: Select all

<?php 
$to  = "email1@hotmail.com" . ", " ; // note the comma 
$to  .= "email1@hotmail.com";
?>
Thats the original... It says note the comma..hmmm... 8O

But I need to add more emails to send it to... so i have to add more lines of:

Code: Select all

$to  .= "email1@hotmail.com";
but i dont know what to do with the adding of the commas.. :cry:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

can you echo the $o variable to see what it contains

Mark
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

Code: Select all

<?php
email1@hotmail.com;email2@hotmail.com;email3@hotmail.com;email4@hotmail.com;email5@hotmail.com;email6@hotmail.com
?>
it doesnt send...

how does PHP want the format of the emails to be like??
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

Code: Select all

<?php
/* recipients */
$to = array(); 
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";
$to[] = "sshh_d0nt_tell@hotmail.com";

$to = implode("; ", $to);

/* subject */
$subject = ":: SHUT UP EMAIL ::";

/* message */
$message = '
<html>
<body>
<p>This Email Has Been Sent From 8th-Dimension . Net from people that hate you.</p>
</body>
</html>
';

/* To send HTML mail, you can set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* additional headers */
$headers .= "To: Some1 <email@hotmail.com\r\n";
$headers .= "From: Mail() PHP Script <rubbish@8th-dimension.net>\r\n";

/* and now mail it */
mail($to, $subject, $message, $headers);
?>
<font color="FF0000">Thank You! Your Email has been sent 5 times to sshh_d0nt_tell@hotmail.com</font>
Thats the whole thing..

Replace those emails with yours, and test out the script.... it doesnt work...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Did you try it with:

Code: Select all

$to = implode(",", $to);
instead of

Code: Select all

$to = implode("; ", $to);
so that you have a comma instead of a semi-colon, sorry I should have checked the manual before posting:
http://php.net/manual/en/function.mail.php
PHP Manual - mail() wrote:Multiple recipients can be specified by putting a comma between each address in to.
If that still doesn't work, have you tried taking out this line:

Code: Select all

$headers .= "To: Some1 <email@hotmail.com\r\n";
and if it's still not working, you may have to loop the e-mail sending script 5 times to get the desired effect.

Mac
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

I've tried all that..... and it doesnt ive an error.. but it doesnt work still...

I don't know how to loop it :(
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Can you send a simple email:

Code: Select all

mail('yourname@youremailhost.com', 'testing e-mail sending', 'just testing this works');
Mac
Post Reply