I'm building a simple order system and want to send an email after the form is submitted. My PHP code looks similar to this:
$name=$_POST["orderName"];
$company=$_POST["orderCompany"];
$email=$_POST["orderEmail"];
$phone=$_POST["orderPhone"];
$headers = "From: $email\r\n" .
$item1=$_POST["orderItem1"];
$qty1=$_POST["orderQty1"];
$item2=$_POST["orderItem2"];
$qty2=$_POST["orderQty2"];
$item3=$_POST["orderItem3"];
$qty3=$_POST["orderQty3"];
$date = date("l, F j Y, G:i") ;
$message="Message sent: $date \n\n
Name: $name\n
Company: $company\n
Email: $email\n
Phone: $phone\n\n
Order:\n
$item1 \tx$qty1\n
$item2 \tx$qty2\n
$item3 \tx$qty3\n";
mail("sales#company.com", "Order", $message, $headers);
That works fine, except in the body of the email I get the value of $item1 string at the very beginning, before the "Message sent..." - just like I added it to the $message (which I don't as far as I can see).
Where you have this:
$headers = "From: $email\r\n" .
you want this instead:
$headers = "From: $email\r\n";
Otherwise, you're concatenating whatever comes on the next line (which happens to be the definition for $item1) to the end of $headers. Although that's not technically valid (i.e., the content is part of the message headers and not body), most e-mail clients will effectively shrug and show it anyway.
Please, please, please add some sanitizing to your POST variables before going with this in production.
Let's see here:
$email=$_POST["orderEmail"];
$headers = "From: $email\r\n";
mail("sales#company.com", "Order", $message, $headers);
I could send a POST request where "orderEmail" contains:
"helo#helo.lv\r\n
From: viagra#farmacety.net\r\n
BCC: victim1#domain1.com, victom2#domain3.com"
etc. and your harmless form would work great for me sending spam to the whole world. This site suggects:
if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) ) {
[... direct user to an error page and quit ...]
}
Related
I have a simple form with a simple mail function:
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$company = $_REQUEST['company'] ;
$phone = $_REQUEST['phone'] ;
$message= $_REQUEST['message'] ;
mail( "$webmaster_email", "Website Form", "From: noreply#mysite.com",
"From: $email \n
Name: $name \n
Phone: $phone\n
Message: $message\n ");
header( "Location: $thankyou_page" );
However, when I test I only rarely receive mail from it and it seems to get stuck - ie: it does not redirect to the thankyou page.
I have placed echos for testing and everything works right up to the mail() function.
I have checked with my hosting provider and there is no mail queue and PHP Apache is working as normal. Obviously, I have also checked my spam folders as well.
Would anyone know why this would only be working some of the time?
You have mixed up the 3rd and 4th parameter of the mail() function. The third parameter should contain the message and the fourth the additional headers. As you are sending the message and all (user provided...) variables in the 4th parameter, that is likely to lead to the problems you are having.
You should be able to change it swapping the parameters:
mail($webmaster_email, "Website Form",
"From: $email \n
Name: $name \n
Phone: $phone\n
Message: $message\n ",
"From: noreply#mysite.com");
You should also add error handling; the mail() function returns true or false depending on successful acceptance for delivery so you could simply log the times it returns false to troubleshoot.
Please bear with me as I'm still very new to this. I'm simply trying to ensure the email message body below has linebreaks where I put <br/>. However when I run the script the message body displays exactly as it is in the script even though it works perfectly in the editor I used to compose this email at Stackoverflow.
I realize its something very small and subtle. /n also didnt seem to work. Thanks for your help! I'm not sure i understand how to use nl2br in my particular context. Again thanks for your patience and help.
$subject=" You're exclusive guide is only a click away!";
$message="Thanks for subscribing .$name! <br/> Click below to confirm your email and email and access your guide <br/> http://acmecorp.net/guide <br/> Acmecorp.net <br />Phone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
mail( $email, $subject, $message,$headers );`
Substitute <br/> with "\n":
$subject=" You're exclusive guide is only a click away!";
$message="Thanks for subscribing .$name! \n Click below to confirm your email and email and access your guide \n http://acmecorp.net/guide \n Acmecorp.net \nPhone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
mail( $email, $subject, $message,$headers );`
You want to use HTML tags in a mail obviously. So, you have to send your mail in HTML, by adding a content type revelant in the headers.
Just before your mail() call, add $headers .= "Content-type: text/html\r\n"; to have something like this:
$subject = " You're exclusive guide is only a click away!";
$message = "Thanks for subscribing .$name! <br/> Click below to confirm your email and email and access your guide <br/> http://acmecorp.net/guide <br/> Acmecorp.net <br />Phone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
$headers .= "Content-type: text/html\r\n";
mail($email, $subject, $message, $headers);
The following PHP code works perfectly, but it is not doing line breaks for some reason.
PHP:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: '".$title."' <".$store_email."> \n";
$subject = "New Payment Received";
//MESSAGE
$message = "New payment was successfully recieved through paypal payment terminal:";
$message .= "\r\n\nFrom ".$paypal->pp_data['payer_email'];
$message .= "\r\nPaid: ".$paypal->pp_data['payment_gross']." ".$paypal->pp_data['mc_currency'];
$message .= "\r\nDate: ".date('d/m/Y');
$message .= "\r\nTime: ".date('g:i A');
mail($admin_email,$subject,$message,$headers);
Any wonder what's wrong? Thanks in advance.
You're sending HTML e-mail. Line breaks have no meaning in HTML, you'll need <br /> tags.
The direct answer ceejayoz gives is correct and to the point in that the html element <br> is needed because it is a html email.
The bigger issue is that not all email is readable in html (example: user doesn't allow html emails). Anyone sending email should send it in 2 parts. One being a html formatted message and the other "alternative" in plain text. In that way the recipient will be able to read the email regardless of email reader.
The \r\n line break works in plain text alternative part and in html<br> or other elements as needed to format.
Doing this will avoid the next question. Recipients are complaining my emails are blank.
I am currently sending text email in PHP and doing something like this:
$from = "from: Hike Attendance Update#comehike.com";
$to_email_address = 'some email';
$subject = 'Some subject';
$contents = 'Blah';
mail($to_email_address, $subject, $contents, $from);
When I send it, it appears as sent from Hike.Attendance.Update which doesn't look very good. How can I make it appear with spaces instead of dots?
Thanks,
Alex
Try:
$from = "from: \"Hike Attendance\" <Update#comehike.com>";
I think that's specified in some RFC for email, namely RFC 5322.
When specifying the actual name, put the email address in <> chars:
$from = "from: Hike Attendance <Update#comehike.com>";
I have a problem when sending a form via email using the PHP Mail function. This is the code that I'm using:
$name = $_POST['name'];
$last_name = $_POST['last_name'];
$company = $_POST['company'];
$email = $_POST['email'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$message = $_POST['message']); //This comes from the form
$formcontent="Name: $name $last_name <br> Company: $company <br> Email: $email <br> Country: $country <br> Telephone: $phone <br><br> Message: $message";
$mailheader = 'MIME-Version: 1.0' . "\r\n";
$mailheader .= 'Content-type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable' . "\r\n";
$mailheader .= 'From: ' . $email . "\r\n";
mail("my#email.com", "subject", $formcontent, $mailheader) or die("Error!");
This is a form that will be sending spanish and special characters, like ñ, accents, ç, etc...
The problem is that, if I use it like this, it works fine in Firefox 3.6.3, but when using the form in Internet Explorer 8, the special characters that sends are all messed up (like ç instead of a ç). However, if I add utf8_encode to the variables in the $formcontent, then it works in IE, but it stops working in Firefox, showing stuff like η instead of ç.
What can I do to make it work regardless of the browser? Any help would be much appreciated!
EDIT:
I've noticed that, if I echo the $formcontent variable before sending it with mail, if I'm using Firefox, the special characters are already messed-up. How can I avoid the browsers interfering with what's being sent? Please I'm totally clueless right now!!! I don't know what to change to have something working. Even if it's a dumbed down version, is there any alternative to have PHP Mail working with special characters in both browsers?
Basically you need to make sure that every charset on your page is the same. Make sure the page has a utf-8 charset aswel. Since you're sending it in utf8 the input must come from utf8 aswell.
Could you perhaps show us the code of the page (or a live demo) where the mail is being made?