php contact form subject encoding - php

I have a contact form and I would like to add the name of the sender when I receive a message from the form.
The variable name is filtered like this:
$newname = htmlspecialchars($name, ENT_QUOTES);
then
mail($to, 'from: ' . $name, $body, $headers);
but my subject show &amp, &lt... instead of & or apostrophes
I did try this from other questions:
mail($to, 'from: ' . "=?UTF-8?B?" . base64_encode($name) . "?=", $body, $headers);
but no luck ...
If I remove the htmlspecialchars() of course it works, but I would like to keep it for protection
EDIT
This is not a duplicate.
Thanks, it's good to know that subject doesn't need to be escaped, but this is not a duplicate because I would like to know how can escape it and still correctly display in the subject (with no ' or &). The
=?iso-8859-1?q?this=20is=20some=20text?=
solution proposed in that answer, makes things even worst in my case, with more weird characters... So, my question still is, why
"=?UTF-8?B?" . base64_encode($name) . "?=",
is not working in my case?
Thanks!

Related

The e-mail header from the online form is displayed incorrectly

I have a questionnaire form on the web. After filling in this form, I send it to me by email, using the PHP function mail(). The form body and the data it contains, including the private message are displayed correctly on gmail.com. The problem, however, occurs in the header of the email itself. Some characters are displayed incorrectly.
Here is a sample header:
$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;
Required display of email subject:
Nový dotaz -- námět, od Fořt Petr <p.fort1990#gmail.com>
Simultaneous displaying of the subject:
Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990#gmail.com>
The squared times symbol is more like a rectangle.
Is anything wrong? Or where should I look for a mistake?
I'm not sure \r\n works on all platforms
see : Which line break in php mail header, \r\n or \n?
instead
("xxx\r\n\yyy");
use
Header('xxx');
Header('yyy');
or use PHP_EOL, not "\r\n"
Problem solved. My hosting provider uses different character encoding for the headers - I can't explain why, but the following php function will do it all.
function recode_to_utf8 ($text, $encoding = "utf-8")
{
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
And now all you have to do is send an email using the mail () method in combination with the method defined above recode_to_utf8(). Like this:
mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));
I hope it helps others if they have the same problem as me.

PHP Mail not working for me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a mail form with PHP and I have something like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'mymail#gmail.com';
$subject = 'Test mail';
$body = "From: $nombre\n E-Mail: $email";
if ($_POST['submit']){
mail ($to, $subject, $body);
header("Location: http://www.example.com/");
}
?>
As you can see the code is really simple, it's just sending a name and an adress but it doesn't work. Whenever I try to send it, the page just change the title to "mypage/send.php" and that's all.
It does not even redirect me to an example page.
I've tried:
Using headers (as recommended by others from here).
Checking if there's a grammatical error.
Testing it in a real page and in localhost with SMPT.
EDIT: HTML USED:
<form method="post" action="send.php">
<input type="text" name="name" placeholder="Your name">
<input type="email" name="email" placeholder="Email">
<input type="submit" value="send">
</form>
Firstly, you don't have an assigned variable called $nombre so having error reporting set would have thrown an undefined variable warning; you may have meant to use $name instead.
Check your Spam since you don't have a proper From: in your header, and/or the Email is being rejected altogether due to that reason, this is a common thing nowadays.
Check that all your form elements are indeed named, this includes your submit button, since your conditional statement depends on this, and you haven't provided your HTML form.
I.e.: name="name" and name="email" and for the submit button name="submit".
It's also better to use isset(). I.e.:
if(isset($_POST['submit'])){...}
instead of if ($_POST['submit'])
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Example from PHP.net:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Add error reporting to the top of your file(s) which will help find errors either.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Example from PHP.net:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
A few things to note are:
Is PHP installed on your server and properly configured?
Is mail installed and properly configured?
If running from your own computer, all of the above apply.
"it's just sending a name and an adress but it doesn't work"
You will need to be more precise with this. "It doesn't work" doesn't define nor describe how it's not working, or any error messages you may have gotten.
If it isn't redirecting, then you may be outputting before header, another common mistake, which is something that error reporting will catch, do use it.
Here is an article on Stack about outputting before header (headers already sent), should you get that warning:
How to fix "Headers already sent" error in PHP
You should check the value of $_POST["submit"] with var_dump and the return value from mail(which is boolean).
Obs.: It is a good pratice to filter all input coming from $_GET and $_POST.

my variable is not echoing <br> tag when it gets to the mail function

I am creating my own contact form and sending all the inputs through variable to the mail function
But there is one error in it......my code is like this in the below...
$from = $_POST["from"];
// sender
$name = $_POST["name"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
$phone = $_POST["phone"];
$mytour = $_POST["select1"];
$howmany = $_POST["select2"];
$arrival = $_POST["arrival"];
$departure = $_POST["departure"];
$accommodation = $_POST["select3"];
$company = $_POST["company"];
// send mail
$messagecontent = "Name:$name , " . "Email:$from ," . "Nature of Enquiry:$subject ," . "Message:$message ," . "Phone No:$phone, " . "My Tour Wish List: $mytour, " . "How many days do you have available:$howmany, " . "Arrival Date:$arrival ," . "Departure Date:$departure ," . "My Preferred Accommodation:$accommodation, " . "Company Name:$company ,";
// $messagewithbreak=str_replace(',',', <br/>;',$messagecontent); // code replaces all your commas with , and a <br> tag
mail("abc#gmail.com", " Contact form Filled", "$messagewithbreak", "From: $from\n");
echo "Thank you for sending us feedback";
}
But when I receive it in the my email...there is not break tag <br> tag...and it receives as....
Name: , Email:abc#gmail.com ,Nature of Enquiry:none ,Message:none ,Phone No:none,My Tour Wish List: Heritage Tour of Kells,How many days do you have available:Half a Day, Arrival Date:2014-09-10 ,Departure Date:2014-09-10 ,My Preferred Accommodation:Hostel,Company Name:none
But I want it to view proper in the email.... as that is not the professional way... to see the email moreover it is difficult to read the email like this.....
what should I do please help me on it....
I will really appreciate your help.
Please answer my question nothing is helping me out here.I have tried..the methods given to me by jenz and rakesh but still the form when receive in the email shows as the paragraph without line breaks....it is getting frustrating
why you concat message string again and again. Also remove double quotes from variables in mail()
$messagecontent="Name:$name , Email:$from , Nature of Enquiry:$subject , Message:$message , Phone No:$phone, My Tour Wish List: $mytour, How many days do you have available:$howmany, Arrival Date:$arrival , Departure Date:$departure , My Preferred Accommodation:$accommodation, Company Name:$company ,";
$messagewithbreak=str_replace(',',',<br>',$messagecontent);
mail("abc#gmail.com"," Contact form Filled", $messagewithbreak, $from);
Properly add formatting to your message. Any HTML tags placed inside double quotes will be converted to the tag when you echo it. So you just need to add <br> in proper places inside the message content. Also all PHP variables inside double quotes will get replaced by its value. So no need of appending each string with double quotes.
$messagecontent="Name:$name <br> Email:$from <br>Nature of Enquiry:$subject
<br>Message:$message<br>Phone No:$phone<br>
My Tour Wish List: $mytour<br>How many days do you have available:$howmany<br> Arrival Date:$arrival <br> Departure Date:$departure <br> My Preferred
Accommodation:$accommodation<br>Company Name:$company ";
To send HTML email you have to add headers which is optional by default where we pass Content-Type type declaration telling mail services that parse this email as HTML.
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
and pass $headers in mail() like,
mail("abc#gmail.com", " Contact form Filled", $messagecontent, $headers);
You are trying to use HTML in a plain text email. If all you want to do is have line breaks, then use \n instead of the br tag.
If you want to use HTML in an email you must add a content-type header. See the PHP mail docs for an example.
// send mail
$messagecontent = "Name:$name \n ";
$messagecontent.= "Email:$from \n";
$messagecontent.= "Nature of Enquiry:$subject \n";
............
...............
Set all the variables like that.

PHP stripslashes variable encoding from contact form

In my PHP contact form script,
I'm using _POST to submit a PHP variable containing quotation marks that have to be escaped, and upon arrival, its value becomes the value of the $formSubject variable in the form, and has its slashes removed with stripslashes.
Here are excerpts from the form showing the $formSubject variable, as well as the headers.
<input type="text" name="formSubject" id="formSubject" value="<?php if(isset($_POST['formSubject'])) echo stripslashes($_POST['formSubject']);?>" />
if (($resp->is_valid) && (!isset($hasError))) {
$emailTo = 'yourEmail#address.com';
$subject = 'A new message from: ' . $formAuthor . ' | ' . $formSubject;
$body = "Email: $formEmail \n\nContent: $formContent \n\n$formAuthor";
$headers = 'From: <'.$formEmail.'>' . "\r\n" . 'Reply-To: ' . $formEmail . "\r\n" . 'Return-Path: ' . $formEmail;
Since I've started using stripslashes on the echoed variable, the subject is correct on the form's page, but once the email arrives, ASCII in subject is not parsing correctly.
THIS IS A \'SUBJECT\'
should be:
THIS IS A 'SUBJECT'
Is this a matter of declaring the correct encoding? This only happened when I started using stripslashes so does stripslashes disregard encoding? I read about an attribute to use on <form> for setting UTF-8, but that didn't work. Is there a fix specific to stripslashes, or is there an overall change I'd have to make for the script encoding?
Can anyone tell me what the issue is?
Thank you for your assistance.

Why won't my HTML/PHP form work?

Ok, so I made a form using HTML, and I'd like to get it to submit the information to my email, so I found and modified this PHP script:
<?php
$to = "me#myemail.com";
$subject = "R&R Form";
$firstname1 = $_REQUEST['firstname1'] ;
$lastname1 = $_REQUEST['lastname1'] ;
$firstname2 = $_REQUEST['firstname2'] ;
$lastname2 = $_REQUEST['lastname2'] ;
$department1 = $_REQUEST['department1'] ;
$department2 = $_REQUEST['department2'] ;
$reason = $_REQUEST['reason'] ;
$behaviour1 = $_REQUEST['behaviour1'] ;
$behaviour2 = $_REQUEST['behaviour2'] ;
$behaviour3 = $_REQUEST['behaviour3'] ;
$behaviour4 = $_REQUEST['behaviour4'] ;
$behaviour5 = $_REQUEST['behaviour5'] ;
$behaviour6 = $_REQUEST['behaviour6'] ;
$behaviour7 = $_REQUEST['behaviour7'] ;
$message = "Nominee: $firstname1 $lastname1 /n Department: $department1 /n /n Nominator: $firstname2 $lastname2 /n Department: $department2 /n /n Reason for nomination: $reason /n /n Additional reasons: $behaviour1 /n $behaviour2 /n $behaviour3 /n $behaviour4 /n $behaviour5 /n $behaviour6 /n $behaviour7 /n";
$headers = "Recognition and Reward Request for $firstname1 $lastname1";
$sent = mail($to, $subject, $message, $headers,) ;
if($sent)
{print "Your nomination was submitted successfully"; }
else
{print "We encountered an error submitting your nomination"; }
?>
It's not very well written, I know (I only started learning php today, and I just modified a script I copy and pasted.), but it doesn't seem to have any syntax errors or any other errors I can see. I'm not asking for someone to fix my code for me, I'm just asking for some pointers as to why the script isn't working as it should.
I uploaded it to a server with PHP installed, so that's not the problem. I've been trying to figure this out all day, and it's getting kinda frustrating. Someone please help?
Well, the script is using this for the headers, which is invalid:
$headers = "Recognition and Reward Request for $firstname1 $lastname1";
Maybe you meant for that to be the subject line?
The headers should be valid SMTP headers, like this:
$headers = 'From: webmaster#example.com' . "\r\n";
Look at the examples for the mail function for more info.
$sent = mail($to, $subject, $message, $headers,) ;
should look like this:
$sent = mail($to, $subject, $message, $headers) ;
(without the comma)
hope i helped
Since you are a starter I recommend you to use PEAR as much as possible.
Look at: pear html quickform
It will really make your life easier.
And for sending e-mails, I suggest you to use: PHPMailer
It comes with a lot of e-mail features just right out-of-the-box
The first problem I see is that $headers doesn't contain valid headers. Headers are things like From: name#example.com or CC: someoneelse#example.com, but you're treating it as part of the email.
Here's some info on email headers.
It does have syntax errors. Since you cannot see them, I suggest you enable full error reporting. There're many ways to do it; the simplest is probably adding this code on top of your script:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
?>

Categories