954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why does this php mail form not work for multiple recipients?

I have a very simple script (below) that works fine when sending email to a single email address, however when I add another (as I have in the script below) by seperating with a comma, the script just sends the email to one of the addresses twice and not to the other one at all. Why might this be please?

<?php

//start building the mail string

$msg = "First name:    $_POST[firstname]\n";
$msg .= "Second name: $_POST[secondname]\n";
$msg .= "Email Address: $_POST[email1]\n";

$thankyoupage = "/signup-thankyou";

//set up the mail
$recipient = "recipient1@test.com,recipient2@test.com";  //the inbox where the sent mail goes    
$subject = "Newsletter signup";
$mailheaders = "From: Website  <recipient1@test.com,recipient2@test.com> \n";
$mailheaders .= "Reply-To: $_POST[email]";
//send the mail
mail($recipient, $subject, $msg, $mailheaders);

header("Location: $thankyoupage");


?>


Thankyou

Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

A few things you can check out:
What's happening in your From: header? Why do you have two email addresses crammed into the <> brackets there? - Youcan use multiple addresses, but they must be formatted the same way the mail() manual entry explains the to field must be formatted.
Emails usually use Windows style line endings: \r\n . - Perhaps not particularly important these days, since modern email clients don't really care, but still worth getting right.
You aren't capturing and validating the return value of the mail() call. It returns a boolean value indicating whether not the mail was accepted for delivery. You should always capture and check it, and if you get FALSE make sure to show/log an error instead of just blindly showing the normal "thank you" page.

Atli
Posting Pro
540 posts since May 2007
Reputation Points: 93
Solved Threads: 70
 

I have read a little of the page suggested by Atli and amended the code as below. However the email still gets sent to 'anotheruser@example.com' twice and not to 'user@example.com' at all. Could anyone give me an example of how to amend my code to get this working please? Thanks

<?php
//start building the mail string
$msg = "First name:    $_POST[firstname]\n";
$msg .= "Second name: $_POST[secondname]\n";
$msg .= "Email Address: $_POST[email1]\n";

$thankyoupage = "/signup-thankyou";

//set up the mail
$recipient = "User <user@example.com>, Another User <anotheruser@example.com>";  //the inboxes where the sent mail should go.     
$subject = "Newsletter signup";
$mailheaders = "From: From  <anotheruser@example.com> \n";
$mailheaders .= "Reply-To: $_POST[email]";
//send the mail
mail($recipient, $subject, $msg, $mailheaders);

header("Location: $thankyoupage");


?>
Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Approx line 10 instead of a comma between the two email addresses have you tried a semi colon?

NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

Thanks, I just tried that bu tthe script sends nothing atall that way.??

Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Whoops sorry, got something confused there, keep the commas, try using the "To: header and be sure to separate your headers with a \r\n instead of just an \n

NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

ie

$mailheaders = "From: From <anotheruser@example.com> r\n";
$mailheaders .= "To: Barry White <test@example.com> , James Franco <test2@example.com> r\n";
$mailheaders .= "Reply-To: $_POST[email]";
NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

Thanks. I tried the following but got nothing. What I am hoping to achieve is for a website user to fill out a form to sign up for a newsletter. The notification email is then sent to 2 email addresses (ie [email]admin1@website.com[/email] & [email]admin2@website.com[/email]).

<?php
//start building the mail string
$msg = "First name:    $_POST[firstname]\n";
$msg .= "Second name: $_POST[secondname]\n";
$msg .= "Email Address: $_POST[email1]\n";

$thankyoupage = "/signup-thankyou";

//set up the mail

$subject = "Newsletter signup";
$mailheaders = "From: From <anotheruser@example.com> r\n";
$mailheaders .= "To: Barry White <test@example.com> , James Franco <test2@example.com> r\n";
$mailheaders .= "Reply-To: $_POST[email]";
//send the mail
mail( $subject, $msg, $mailheaders);

header("Location: $thankyoupage");


?>
Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

try this:

<?php
    //start building the mail string
    $msg = "First name: $_POST['firstname']\n";
    $msg .= "Second name: $_POST['secondname']\n";
    $msg .= "Email Address: $_POST['email1']\n";
    $to = "admin1@website.com , admin2@website.com";
    $thankyoupage = "/signup-thankyou";
     
    //set up the mail
     
    $subject = "Newsletter signup";
    $mailheaders = "From: From <anotheruser@example.com> \r\n";
    $mailheaders .= "Reply-To: $_POST['email']";
    //send the mail
    mail($to, $subject, $msg, $mailheaders);
     
    header("Location: $thankyoupage");
     
     
    ?>
NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 
[*]Emails usually use Windows style line endings: \r\n . - Perhaps not particularly important these days, since modern email clients don't really care, but still worth getting right.

I disagree, all mail that I send uses \n line endings, and I have tested proper operation in most popular web, Windows, and Linux mail clients. Checking my Thunderbird Inbox (on Kubuntu) I see that it is of type "fileformat=unix" so that implies \n as well.

dotancohen
Junior Poster in Training
65 posts since Jan 2011
Reputation Points: 10
Solved Threads: 4
 

Using the code from previous reply I get:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/path_to_file/contact-response.php on line 3


?

Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

did the two emails go through?

NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

No.

Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

ok try this then

<?php
    //start building the mail string
    $msg = "First name:".$_POST['firstname']."\n";
    $msg .= "Second name:".$_POST['secondname']."\n";
    $msg .= "Email Address:".$_POST['email1']."\n";
    $to = "admin1@website.com , admin2@website.com";
    $thankyoupage = "/signup-thankyou";
     
    //set up the mail
     
    $subject = "Newsletter signup";
    $mailheaders = "From: From <anotheruser@example.com> \r\n";
    $mailheaders .= "Reply-To:".$_POST['email']."";
    //send the mail
    mail($to, $subject, $msg, $mailheaders);
     
    header("Location: $thankyoupage");
     
     
    ?>
NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

All working now. Thank you so much for your help :)

Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Sorry sometimes I get coding too fast and forget small syntax issues while posting glad to have helped.

Alternatively you could have assigned the $_POST variables to actual non global variables and used the php form to verify that these were legitamate postings. I have a honeypot script that uses a final field that is hidden by css from users and if when I process the form I notice that there is content in there it does not generate an email.

NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

Thanks for your help on this NijaMediaD. Your honeypot script sounds like something I could use. How would I go about implementing that with the script you've suggested so far?

Surfsup
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

its really simple most bots do not parse css. so in your form (before the form closing tag ) put a textbox in a div like so:
[HTML]

URL:




If you can see this text and the field above do not fill it out. Please switch to a browser that supports CSS.




[/HTML]
in the css sheet you would put

#hp {
visibility:hidden;
}

finally would put this in the php handler (where you process the email)

if($_POST['xtext'] == ''){

//put all your email processing here

}
else
{
header("Location: index.php"); //put the page you want the bot to return to here.
	exit;
}
NinjaMediaD
Light Poster
39 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: