styling emails that come in from a php form - php

I have a form on a site that allows the user to enter their name, phone, email etc. When I receive this information as an email it comes in completly unformated, so it's a little harder to read. See image below:
Is there a way I can style this using CSS, ie. make the From and email headings bold {font-weight:bold;}?
The php I'm using for the form is:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent ="From: $name \n Email: $email \n Phone: $phone \n Message: $message";
$recipient = "studio#ll-i.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<p>Thanks for getting in touch, we'll get back to you shortly..</p>";
?>

You can format it easily enough in HTML like this - note that you can write internal CSS that will be used in the content:
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
<style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
h3
{
color:red;
text-align:left;
font-size:8pt;
}
</style>
</head>
<body>
<h3>The fancy CSS heading!</h3>
<p>".$email."</p>
<hr>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
";
// You can even do stuff like this:
for($i=0;$i<count($someArrayFromYourForm);$i++)
{
$email.="
<tr>
<td>".$someArrayFromYourForm['formField']."</td>
<td>".$someArrayFromYourForm['formField2']."</td>
</tr>
";
}
$email.="
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

You have to style it with inline CSS / CSS embedded in the of a HTML file.
The $formcontent variable can contain HTML e.g.
"<html>
<head>
<title>Message</title>
</head>
<body>
<p><strong>From:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Phone:</strong> $phone</p>
<p><strong>Message:</strong> $message</p>
</body>
</html>"
Put the CSS in the head of this like so:
<head>
<style type="text/css">
body { background-color: #ff0000; }
</style>
</head>
Obviously you'll have to use these: ' instead of these " and concatenate any variables using either a full stop or comma incase you need to use speech quotes for any of the HTML.
Like this:
'<html>
<head>
<title>Message</title>
<style type="text/css">
body { background-color: #ff0000; }
</style>
</head>
<body>
<p><strong>From:</strong> ',$name,'</p>
<p><strong>Email:</strong> ',$email,'</p>
<p><strong>Phone:</strong> ',$phone,'</p>
<p><strong>Message:</strong> ',$message,'</p>
</body>
</html>'
EDIT:
You should also change your headers like so:
$mailheader = "MIME-Version: 1.0" . "\r\n";
$mailheader .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$mailheader .= "From: $email \r\n";

Related

PHP mail function sending the email, but not the html form data

The HTML consists of simple input fields as listed in the php below, what happens is PHP mail() sends the email but excludes the form data itself. I have spent a lot of time but not sure why this is happening.
and the php to send the mail:
<?php
$to = "user#gmail.com";
$subject = "Question Bank - New Question";
$message = '<html>
<head>
<title>New Question</title>
<style>
table {
border: 1px solid black;
}
table th, tr{
text-align:center;
}
</style>
</head>
<body>
<table cellspacing="20px">
<tr>
<th>Username</th>
<th>Email</th>
<th>Question</th>
<th>Question Hint</th>
<th>Correct Answer</th>
</tr>
<tr>
<td>'.$_POST["username"].'</td>
<td>'.$_POST["email"].'</td>
<td>'.$_POST["question"].'</td>
<td>'.$_POST["questionhint"].'</td>
<td>'.$_POST["answer"].'</td>
</tr>
</table>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: user#company.com' . "\r\n";
$headers .= 'Cc: cc#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

Adding style to a php html email

I looked all over for this question and all I found on this was to add the following:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
and
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I am wanting to send a newsletter type email, so the styling really matters for this. All of the videos I watched were just making html sheets, so I really didn't get that. I want to style the content in my email.
I have this right now:
$to = $newsletter_email;
$subject = 'Thank you for subscribing';
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
#email-wrap {
background: #151515;
color: #FFF;
}
</style>
</head>
<body>
<div id="email-wrap">
<p>Hi,</p><br>
<p>Thank you.</p><br>
<p>Thank you,</p>
<p>Administration</p>
</div>
</body>
</html>
';
$from = "newsletter#example.com";
//$Bcc = "example#example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
// $headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
I have tried taking out the style from this message variable and turning this file into a html styled file, outside of the php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Newsletter</title>
<style>
#email-wrap {
background: #151515;
color: #FFF;
}
</style>
</head>
etc.
The email actually sends, but I cannot figure out how to add style to this. What am I doing wrong??
$email_from = "newsletter#example.com";
$full_name = 'Company Name';
//$from_mail = $full_name.'<'.$email_from.'>';
$from = $from_mail;
//$from = "newsletter#example.com";
//$Bcc = "example#example.com";
// To send HTML mail, the Content-type header must be set
$headers .= "From: ".$full_name." <".$email_from.">\r\n";
and $headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n";
/*$headers = "" .
"Reply-To:" . $from . "\r\n" .
"X-Mailer: PHP/" . phpversion();*/
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from_email. "\r\n";
// $headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
You need to use inline style to get it works on your email
$to = $newsletter_email;
$subject = 'Thank you for subscribing';
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<div id="email-wrap" style='background: #151515;color: #FFF;'>
<p>Hi,</p><br>
<p>Thank you.</p><br>
<p>Thank you,</p>
<p>Administration</p>
</div>
</body>
</html>
';
$from = "newsletter#example.com";
//$Bcc = "example#example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
// $headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
For the second part of your question you can use something like this
$to = 'test#server.com';
$email_from = "best.buy#yahoo.com";
$full_name = 'Best Buy';
$from_mail = $full_name.'<'.$email_from.'>';
$from = $from_mail;
$headers = "" .
"Reply-To:" . $from . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from_email . "\r\n";
mail($to,$subject,$message,$headers);
Just try to make it like template.
Here is the little bit example may be helpful.
Create my_template.html file & add following code in that file.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Newsletter</title>
<style>
#email-wrap {
background: #151515;
color: #FFF;
}
</style>
</head>
<body>
<p>Hi {{USERNAME}}</p>
<p>Please click on below link </p><br>
{{LINK}}
</body>
</html>
now Write send email function where you want. and follow the step :
1) Read HTML file you recently created.
$html = file_get_contents('my_template.html');
2) Replace Variables
$link = "<a href='LINK YOU WANR TO PLACE'>Click Here </a>";
$html = str_replace("{{USERNAME}}",$username,$html);
$html = str_replace("{{LINK}}",$link,$html);
3) Finally send email.
$from = "newsletter#example.com";
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$html,$headers);
Indeed, the emails that you send will be opened in different ways, either through an email client installed on the machine that will integrate an HTML rendering engine propriƩaitre, or an online e-mail client that will your emails in a rather special roping and withdraw some HTML attributes.
The table proposed by Campaign Monitor gives you a very quick overview of the extent of damage and we note that, according to the email clients, rules are completely different.
link : https://www.campaignmonitor.com/css/
However there are tools such as inline-gulp-css that can turn your HTML styles making them "online".
link : https://www.npmjs.com/package/gulp-inline-css
TRY
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<div style="background:#151515; color: #FFF;">
<p>Hi,</p><br>
<p>Thank you.</p><br>
<p>Thank you,</p>
<p>Administration</p>
</div>
</body>
</html>

php mail syntax error

$mail_body = '<html>
<body style="background-color:#CCC; color:#000; font-family: Arial, Helvetica, sans-serif; line-height:1.8em;">
<h3><img src="http://i.imgur.com/OODKT9h.png" alt="GR" width="194" height="123" border="0">
</h3>
<p>Hello ' . $name . ',</p>
<p>You can make this out to be just like most any web page or design format you require using HTML and CSS.</p>
<p>Grill on the Rock </p>
<hr>
<p>To opt out of receiving this newsletter, click here and we will remove you from the listing immediately.</p>
</body>
</html>';
$to = "$email";
$subject = "Example Grill on the Rock Email";
$from="info#grillontherock.com";
$mail_result = mail($to, $subject, $mail_body, "From:".$from);
}
if($mail_result){
echo "Email has been sent successfully";
}
I am having problem with sending email with php and html.
This code works perfectly fine but the email I am getting is
but when I use double quotation for html file php code greys out for some reason.
$mail_body = "<html>
<body style="background-color:#CCC; color:#000; font-family: Arial, Helvetica, sans-serif; line-height:1.8em;">
<h3><img src="http://i.imgur.com/OODKT9h.png" alt="GR" width="194" height="123" border="0">
</h3>
<p>Hello ' . $name . ',</p>
<p>You can make this out to be just like most any web page or design format you require using HTML and CSS.</p>
<p>Grill on the Rock </p>
<hr>
<p>To opt out of receiving this newsletter, click here and we will remove you from the listing immediately.</p>
</body>
</html>";
$to = "$email";
$subject = "Example Grill on the Rock Email";
$from="info#grillontherock.com";
$mail_result = mail($to, $subject, $mail_body, "From:".$from);
}
if($mail_result){
echo "Email has been sent successfully";
}
a bit new to php and html and I could not find similar problem at the moment.
Also, Is it possible to have email as html form and bring that html through send php file?
You need to set Content-Type to text/html in mail headers
Example headers with Content Type:
$headers = "From: danny#danny.domain\r\n";
$headers .= "Reply-To: no-reply#danny.domain\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
then
$mail_result = mail($to, $subject, $mail_body, $headers);
#edit.
Also check exaple #4 at:
http://php.net/manual/en/function.mail.php
With second html you are having problem with double quotes, because you are using double quotes inside the html, try to escape them. Try this:
or try the link below:
What is the difference between single-quoted and double-quoted strings in PHP?
You need to set Content-Type to text/html in mail headers to send your mail as html mail.
Example headers:
$headers = "From: mymail#gmail.com\r\n";
$headers .= "Reply-To: no-reply#mymail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
finally after your content you need to send mail like the following.
$mail_result = mail($to, $subject, $mail_body, $headers);
Then for your double quotes problem : here you used double quotes inside double quotes. if that so you need to escape it with "/".

Sending email in HTML [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I am setting up a confirmation email script for our customers to receive an email when they complete the online form. I uploaded my script and tested but I received an email with just the raw HTML. I have been looking at some tutorials trying to do this as well. I validated my HTML and I passed all the tests. Is there something that I have to include in my PHP for my body string to be interpreted as HTML?
Here is my script
<?php
//Script for sending a confirmation email
$body = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<title>Demystifying Email Design</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
</head>
<body style=\"margin: 0; padding: 0; background-color: #ecf0f1;\">
<table cellpadding=\"0\" cellspacing=\"0\" width=\"60%\" align=\"center\" style=\"border: 1px solid #bdc3c7;\">
<tr bgcolor=\"#3498db\">
<td align=\"center\">
<img alt=\"Logo\" src=\"http://www.****.com/logo-lg.jpg\" style=\"width: 50%; height: auto; padding-top: 5%; padding-bottom: 5%;\" />
</td>
</tr>
<tr bgcolor=\"#ffffff\" align=\"center\">
<td>
<h3 style=\"padding-top: 5%; padding-bottom: 5%;\">Worldwide innovator in flexible liquid packaging</h3>
</td>
</tr>
<tr align=\"center\" bgcolor=\"#ffffff\">
<td>
<h4 style=\"padding-top: 5%; padding-bottom: 5%;\">Thank you for contacting customer service. We've received your sample request; one of our team members will be in contact with you in the very near future. Thanks again for your time and interest.</h4>
</td>
</tr>
</table>
</body>
</html>";
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
if (mail($to,$subject,$body)){
echo "Mail was sent";
} else{
echo "Failed";
}
This is the email I received
You need to also set a header indicating this is HTML, e.g.
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Source (see Example 3).
Correct code will be:
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if (mail($to,$subject,$body,$headers)){
echo "Mail was sent";
}

HTML E-Mail as fileattachment

I have a Problem with Outlook 2010.
I sent an E-Mail with a Contactform with this Code:
$message = '
<html>
<head>
<title>Anfrage ('.$cfg->get('global.page.title').')</title>
<style type="text/css">
body { background:#FFFFFF; color:#000000; }
#tbl td {
background:#F0F0F0;
vertical-align:top;
}
#tbl2 td {
background:#E0E0E0;
vertical-align:top;
}
</style>
</head>
<body>
<p>Mail von der Webseite '.$cfg->get('global.page.title').'</p>
<table id="tbl">
<tr>
<td>Absender</td>
<td>'.htmlspecialchars($_POST['name']).' ('.htmlspecialchars(trim($_POST['email'])).')</td>
</tr>
<tr id="tbl2">
<td>Betreff:</td>
<td>'.htmlspecialchars($_POST["topic"]).'</td>
</tr>
<tr>
<td>Nachricht:</td>
<td>'.nl2br(htmlspecialchars($_POST["message"])).'</td>
</tr>
</table>
</body>
</html>';
$absender = $_POST['name'].' <'.$_POST['email'].'>';
$header = "From: $absender\n";
$header .= "Reply-To: $absender\n";
$header .= "X-Mailer: PHP/" . phpversion(). "\n";
$header .= "X-Sender-IP: " . $_SERVER["REMOTE_ADDR"] . "\n";
$header .= "Content-Type: text/html; Charset=utf-8";
$send_mail = mail($cfg->get('contact.toMailAdress'), "Anfrage (".$cfg->get('global.page.title').")", $message, $header);
//$send_mail = mail("jonathan.sigg#studcom.ch", "Anfrage (".$cfg->get('global.page.title').")", $message, $header);
$_SESSION['kontakt_form_time'] = time();
$tpl->assign("mail_sent", $send_mail);
When I sent the email, doesn't shows the message. it generates a File named [NAME].h. The Message is in this File. How can I fix that, that the message shows in the E-Mail. Is this a Problem about the settings in Outlook?
The problem is with your security settings in Outlook. You need to change the settings to display HTML.
Be careful though, other people trying to read this message will have the same problem. It's common courtesy to offer a plain text version in addition to a HTML version so those that receive these emails don't have to compromise their security settings for you. There's a good chance messages like this can be marked as Spam too.
Good luck.

Categories