how to format message body of email? - php

i have developed a simple php contact form, that's working fine, but i am unable to format the body of the message as per my requirement, my code is given below, i am getting mail in a single line format,
where i want every information on a new line like this
"Name: Syed Sheeraz Ahmed
Contact No: 03453594552
Email: abc#abc.com
Address: R-47, Sector 9, North city.
Requirement: hello how are you"
<?php
$to = "sheery_1#hotmail.com";
$subject = "From Website Contact Form";
$name = $_REQUEST['name'] ;
$contact = $_REQUEST['contact'] ;
$address = $_REQUEST['address'] ;
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>";
$MESSAGE_BODY .= "Contact No: ".$_POST["contact"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Address: ".$_POST["address"]."<br>";
$MESSAGE_BODY .= "Requirement: ".nl2br($_POST["message"])."<br>";
$message = $_REQUEST['message' + 'address' + 'contact'] ;
$from = $_REQUEST['email'] ;
$headers = "From:" . $from;
mail($to,$subject,$MESSAGE_BODY,$headers);
echo "Mail Sent.";
?>

You want a new line (\n) not an HTML line break (<br>) since your email isn't marked as having an HTML body (and emails that have HTML bodies should really have multipart MIME bodies with both plain text and HTML versions since "HTML only" is a nice flag for spam detectors).

As you are using html tag in your email content.
Set the content- type text/html in the header of your mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
OR
you can save yourself from this type of problems using phpmailer

If your email is sending as text then the tags will do nothing. Try using a new line character for example:
$MESSAGE_BODY = "Name: ".$_POST["name"]."\n";
That should sort your problem.
in above example <br> is printed after the variable printed, why? how to remove <br>
for example:
$_Post["name"] has value "jhon".
it prints: jhon<br>

Related

How to show new lines on enter in mail from PHP contact form? [duplicate]

This question already has answers here:
linebreak in html php email
(2 answers)
Closed 3 years ago.
The form is working, but the textarea input has no new lines.
The whole message is showed on a single line.
FYI, my textarea has no CSS. And I have changed the email adress for privacy purposes.
Thanks in advance,
Sam
<?php
$emailAddress = $_POST["email"];
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$textMessage = $_POST["message"];
$to = "contact#website.com";
/*standard headers for HTML mail*/
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
/*Add. headers*/
$headers .= 'From: ' . $emailAddress . "\r\n";
$headers .= 'Reply-to: ' . $emailAddress . "\r\n";
$headers .= 'First name: ' . $firstName . "<br>";
$headers .= 'Last name: ' . $lastName . "<br><br>";
$headers .= 'Message:'."<br><br>";
$status = mail($to, "New client inquiry", $textMessage, $headers);
if($status)
{
echo "<p>Your mail has been send succesfully!</p>";
}
else
{
echo "<p>Your mail has not been delivered, please try again</p>";
}
?>
You set the content type of mail as text/html, so it expects the content to be valid HTML.
As you should know, newlines have no effect on html rendering. BR or P tags should be used.
Either transform your message in proper HTML (replacing newlines with BR) or change the content type to text/plain.
By the way CSS has nothing to do here (won't alter the content of value sent during request).

add image to PHP email script

I've done some research on this issue but still can't find an answer that I understand. Basically I'm trying to add a Logo to the emails sent via my email PHP script. The code of my PHP script is:
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$sub = $_POST['Subject'];
$message = $_POST['Message'];
$Subject = "$sub";
$Sendto = "Omitted for privacy reasons";
mail($Sendto,$Subject,
"$message
Email: $email
Name: $name
"
,"From: $email,$name");
?>
How do I go about putting an image held on my server in Images/logo above the email:$email part of the message?
thanks
As a side question can anyone provide me with a guide or reading on how to beautify the message as its so plain
What you might do is to send the mail not as "text/plain", but as "text/html".
You e-mail content is then basically a static webpage and you can insert images to.
Example Code for Mail with HTML Content
$to = 'to#someone.com';
$subject = 'The Subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: other#someone.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
// this is the important header to set the type
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// now $message can be a static html page like:
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';
// with image embedded
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="http://i.stack.imgur.com/Qmw3Z.jpg?s=32&g=1" alt="AVTR" />';
$message .= '</body></html>';
Important: If you insert something into the message coming from POST, do not forget to escape it properly with htmlentities($_POST['somevar']) or striptags.
Then send mail:
mail($to, $subject, $message, $headers);
Answer to side-quest: "can anyone provide me with a guide or reading on how to beautify the message as its so plain". When you send the mail as HTML you might apply CSS styles to your HTML.
Starter tutorial: http://webdesign.tutsplus.com/articles/build-an-html-email-template-from-scratch--webdesign-12770
you might also google for "html email templates", download one and modify to your needs

Add recieving email adress in contact form

I'm working with contact form
This is part where email is being sending
$emailTo = 'myemail#gmail.com';
$subject = 'Wiadomość ze strony Mud';
$sendCopy = trim($_POST['sendCopy']);
$body = "Email: $email \n\nKind of frame: $frame \n\nColor suggestion: $color \n\nInfo about project: $comments";
$headers = 'From: ' . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
Form works good everything is fine, except when I get mail and I want IN MY MAILBOX reply email it shows adress of my webserver
It looks like $headers dont work properly, If I'm right this part of code is responsible for reply adres email "'Reply-To: ' . $email;"
(IM NOT GOOD IN PHP)
And my second optional question is how to make stylish email.
I mean for example in my email when it comes to mailbox it shows
Email: dsadas#dsdas.pl
Kind of frame: dasdsa
Color suggestion: dasdsa
Info about project: dasdsa
How can I do it bold, color etc ?
THANK YOU FOR YOUR TIME, CHEERS
Here you go for headers that will allow HTML for email and fix your reply to email:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $emailTo <$emailTo>" . "\r\n";
Remember that there is limited support for email when it comes to CSS, you can see things that can't be used here: http://www.campaignmonitor.com/css/

How to customize the way an email looks sent from a php mail() script

So I can't seem to word the question right - but I would like to know how would you go about changing the way this
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
looks when it reaches my e-mail. When I receive the e-mail (using it as a contact form), I get this ->
Name: lloan
City: SomeCity
Email: some#email.com
Message: Testing
I tried <strong> $Body .= "Name: ";</strong> and that didn't work - I also tried it as $Body .= "<strong> Name: </strong>"; and again, nothing. Can anyone point me in the right direction? I really want to learn how to do this - I've googled it a couple of times, but I think it's the way I'm wording my question that's not helping me.
EDIT: So I'm able to do this now - $Body .= "<strong>Name: </strong>"; using the suggestions below of adding the headers and then adding that to the mail() function - however, how do I go about using it on a bigger scale? For example - What if I want to have a colored background or what not - Am I also able to use CSS in this kind of situation? The other questions on SO do not answer this in it's entirety.
Here's how:
$to = 'yourmailtarget#example.com';
$subject = 'Your email subject';
$headers = "From: abc#abc.com \r\n";
$headers .= "Reply-To: no-reply#abc.com \r\n";
$headers .= "CC: abc#abc.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = 'Put your html message here<br>';
$message .= "<b>HTML feature is now enabled</b><br>";
$message .= "<table border="1"><tr><td>Hello</td><td>World</td></tr></table>";
$message .= "For the styling, you can use inline styling:<Br>";
$message .= "<span style='color:#FF0000;'>This is red color</span>";
mail($to,$subject,$message,$headers,$parameters);
The most important part to answer your question is this line:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
That tells the system to produce the email using the HTML format.
Hope this helps
In the mail function add a parameter headers like this :
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and call the mail function like this :
mail($to, $subject, $Body, $headers)
If you would like to format your message (the content of variable $Body), you are free to use as much HTML as you want.
If you want to send HTML email you also need to send a text-only version as not all mail clients support HTML, and some mail systems specifically strip HTML versions.
This means you need a Content-type:multipart header, and multiple sections with their own headers, each delimited by a specific boundary. The actual format is quote complex, so use a library like swiftmail or PHPMailer

PHP line breaks don't seem to work

I have the following code that sends a message using the mail() function. Everything works fine except the line breaks at the end of each line don't seem to work. As you can see I am using " \r\n " hoping that this will give me a line break but it doesn't I have added <br> to get the break, but I would rather not use that in case someone doesn't have an HTML email client.
<?php
$to = 'user#example.com'; // Was a valid e-Mail address, see comment below
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$content = 'Name: '.$name."\r\n";
$content .= 'Email: '.$email."\r\n";
$content .= 'Subject: '.$subject."\r\n";
$content .= 'Message: '.$message."\r\n";
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
// Additional headers
$headers .= 'To: iVEC Help <help#ivec.com>'. "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
mail( $to, $subject, $content, $headers);
?>
<p> You sent it ... good work buddy </p>
<p> <?php '$name' ?> </p>
You're sending it as HTML - change the content type to text/plain and it should work.
The problem is that you say, in your headers, that the mail is an HTML email:
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
If you want to ensure compability with both HTML and text clients, consider using the Multipart content type. This can be achieved in many different ways, but one simple way is to use a library that can do this for you. For example, you can use PEAR:Mail_Mime
I'm not an expert in this area, but my guess would be that you're setting the content type to text/html, which implies html-rendering (which means line breaks are translated to a space). If you're not using any HTML-elements (which appear not to), try setting the content type to text/plain.
As mentioned before, either set it to text/plain or add a HTML break for line breaks:
$content = 'Name: '.$name."</br>\r\n";

Categories