开发者

php mail multiple recepients

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开发者_Python百科 those ids. I am yet to figure out how to comma/space seperate the emailds but will try it in google search.

thanks


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:

    * user@example.com
    * user@example.com, anotheruser@example.com
    * User <user@example.com>
    * User <user@example.com>, Another User <anotheruser@example.com>

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


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('address1@example.com');
$mail->AddAddress('address2@someotherplace.com');
etc...


Separate their addresses with commas.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜