Jump to content


Sending A Message To Multiple Email Accounts


11 replies to this topic

#1 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 09 November 2006 - 07:22 PM

I need a php script that will allow me to send a message typed in a message box to multiple emails in another box seperated by comma's.

#2 BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 09 November 2006 - 07:30 PM

You can follow a tutorial on the site to create a simple form. Once you've done that, take a look at this code

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
   <tr>
	 <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
   </tr>
   <tr>
	 <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
   </tr>
   <tr>
	 <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
   </tr>
  </table>
</body>
</html>
';

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

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

See how the have $to posted twice. Thats a hint! I don't want to give you the code because you won't learn anything so read this

#3 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 09 November 2006 - 10:44 PM

You could do something like this as well.
<?php
$emails = explode(', ', trim($_POST['emails'])); // You should also do any other verification with this right here, if needed, personally I would maybe use JS to peruse them before the user submits them aswell

$headers = 'From: Yoursite <you@yoursite.ext>';

foreach($emails as $e)
if(!mail($e, 'Your Subject', 'Message', $headers)) break;
?>

Edited by Mr. Matt, 10 November 2006 - 01:09 AM.


#4 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 11 November 2006 - 05:01 PM

i have over 3000 emails to send a letter to im not going to put $to 3000 times. That why i need the script to send it to the ones in the box seperated by comas.

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 12 November 2006 - 12:31 AM

Assuming you're using a database to organize the e-mails, why don't you just use a loop?
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0'."\r\n"
		   .'Content-type: text/html; charset=iso-8859-1'."\r\n";

// Additional headers
$headers .= 'To: ';
while($r=mysql_fetch_array($sql)){
	$headers .= $r['name'].' <'.$r['email'].'>, ';
}

$headers = substr($headers, 0, -2)."\r\n" // Substr() to remove additional comma and space.
		  .'From: Birthday Reminder <birthday@example.com>'."\r\n";

// Mail it
mail($to, $subject, $message, $headers);


#6 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 12 November 2006 - 08:58 AM

nope no database the guy who owns the site doesnt remember where he bought his hosting. So thats why again i need the emails entered in the box that are seperate by comma's to send a message to them.

#7 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 12 November 2006 - 09:41 AM

That's even easier :laugh:

Ok so say someone inputs emails like this into the box:
email1@domain.com, email2@domain.com,email3@domain.com

You can explode it which returns an array.
$emails = explode(",",$_POST['emails']);

Now we want to mail to each email in the list so we do this:
foreach($emails as $value)
{
	$to = trim($value); 
	// remove white space incase someone enters a space after/before comer

	mail($to, $subject, $message, $headers);
}

That would mail the email to each person separately, but if you wanted it how rc posted just change the action within the loop.

foreach($emails as $value)
{
	$to = trim($value); 
	// remove white space incase someone enters a space after/before comer

	//  $names = explode("@", $to);
	//  $headers .= $name[0].' <'.$to.'>, ';
	//  Use above instead to use first part of email as name as no name available.

	$headers .= $to.' <'.$to.'>, ';
}


$headers = substr($headers, 0, -2)."\r\n" // Substr() to remove additional comma and space.
		  .'From: Birthday Reminder <birthday@example.com>'."\r\n";

// Mail it
mail($to, $subject, $message, $headers);

On another note...

Quote

the guy who owns the site doesnt remember where he bought his hosting
I have to ask how you forget something like that....even if you did there is whois :laugh:

Edited by Matthew., 12 November 2006 - 09:46 AM.


#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 12 November 2006 - 12:38 PM

Don't ya love how you post the code, the person asks the exact same question again, and 2 other people post the exact same code?

Also, what/why did Mr. Matt edit my last post? I don't remember putting anything offensive or anything of the sort...

Edited by Demonslay, 12 November 2006 - 12:41 PM.


#9 Hip Hop Artist

    Young Padawan

  • Members
  • Pip
  • 152 posts
  • Gender:Male
  • Location:Canada

Posted 12 November 2006 - 01:15 PM

k well thanks Mr. Matt you explained it to me better. You didnt just throw some code in my face.

#10 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 12 November 2006 - 02:33 PM

View PostDemonslay, on Nov 12 2006, 05:38 PM, said:

Don't ya love how you post the code, the person asks the exact same question again, and 2 other people post the exact same code?

Also, what/why did Mr. Matt edit my last post? I don't remember putting anything offensive or anything of the sort...

He was probably just fixing a mistake in your code :)

#11 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 12 November 2006 - 06:07 PM

View PostMatthew., on Nov 12 2006, 07:33 PM, said:

View PostDemonslay, on Nov 12 2006, 05:38 PM, said:

Don't ya love how you post the code, the person asks the exact same question again, and 2 other people post the exact same code?

Also, what/why did Mr. Matt edit my last post? I don't remember putting anything offensive or anything of the sort...

He was probably just fixing a mistake in your code :)

exactly

#12 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 12 November 2006 - 07:07 PM

View PostDemonslay, on Nov 12 2006, 05:38 PM, said:

Don't ya love how you post the code, the person asks the exact same question again, and 2 other people post the exact same code?

Ha! I've only just understood that reply...i didn't even see your post :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users