1,105,625 Community Members

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

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Atli
Practically a Master Poster
691 posts since May 2007
Reputation Points: 182 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 16 [?]
 
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? - You can 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.
Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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");


?>
Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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]";
Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 admin1@website.com & admin2@website.com).

<?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");


?>
Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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");
     
     
    ?>
Member Avatar
dotancohen
Junior Poster in Training
64 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

[*]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.

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

?

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

did the two emails go through?

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

No.

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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");
     
     
    ?>
Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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:

<div id="hp">
<p>URL:<input name="xtext" type="text" value="" /></p>
<br/>
<p>If you can see this text and the field above do not fill it out.  Please switch to a browser that supports CSS.</p>
</div>

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;
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: