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

Recommended Answers

All 17 Replies

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.

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


?>

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

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

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

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

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


?>

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

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

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

?

did the two emails go through?

No.

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

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

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.

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?

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.