tags:

views:

157

answers:

4

Hi, I am able to send email to one email id by defining the id in the mailer, but i am not able to understand how to send to multiple recepients when an user types "mssage and email id's" in a form. Ex - I am showing a form with two text areas - one for email id's and one for custom message. So when they click send, i want to take the email id's from that text area and send that message to those ids. I am yet to figure out how to comma/space seperate the emailds but will try it in google search.

thanks

+2  A: 

The documentation of the to parameter for the mail() function states:

Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822. Some examples are:

    * [email protected]
    * [email protected], [email protected]
    * User <[email protected]>
    * User <[email protected]>, Another User <[email protected]>

http://php.net/manual/en/function.mail.php

Update:

I've come across this great tutorial on sending mail with PHP: http://articles.sitepoint.com/article/advanced-email-php

George Marian
hi,thanks, i managed to send multiple emails and i am planning to post the code sometime today once i clean it up little bit. now i am stuck with sending automated emails via cron. this is what i need - while the admin send emails, i store the message, emails, event date in the database. now i am trying to set acron job to send emails to all these ids from the table with the message i have as a reminder. i am not familiar with cron job scripting, can anyone help in guiding me the right way to write scrip that i can place in cron tab.thanks
Jay
@Jay You should accept an answer to this existing question (your accept rate isn't very high) and post a separate question for your question about running it through cron. (Ideally, you should search for an answer first.)
George Marian
@george,I agree with your point, as I mentioned in one other question, I definitely give credits once I am done with my work based on the answers and comments. May be there is a delay but I do it without fail. I will post my cron job question in a bit. thanks. I added that as a question here http://stackoverflow.com/questions/3368307/how-to-send-emails-via-cron-job-usng-php-mysql
Jay
+1  A: 

Separate their addresses with commas.

no
+1  A: 

It's far easier to use something like PHPMailer (free, easy to install, easy to use) if you have to do anything even "moderately" complicated, like multiple recipients for an email. It'll hide all the ugly details for you behind a nice interface. Instead of worrying about header syntaxes and whatnot, you just do something like:

$mail = new PHPMailer();
$mail->AddAddress('[email protected]');
$mail->AddAddress('[email protected]');
etc...
Marc B
+1 a good idea, though I think it's essential to know what's going on behind the scenes.
George Marian
A: 

hi,

after doing some search, I came with this answer to send multiple address seperated by semicolon(for some reason my try with comma in this code failed, but i am fine with semicolon).

If in case if anyone is looking for JS validation to check the id's entered

var emailRegex = /^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/;

As i collected the email id's separated by semicolon, i had to replace ; with ,

$emails_tosend = preg_replace('/;/', ',', $emailid);

$email id - collected from the form

so after doing the above replacement, i just use the normal "to" in the mailer to send to multiple recipients.

$to = "$emails_tosend";
mail ($to, $subject, $message, $headers);

hope this helps and it's clear. I can explain if anyone needs more clarification. thanks for all your help.

Jay