Adding style to a php html email - php

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>

Related

HTML within PHP not showing up as HTML

I tinkered with an existing php email file to add the confirmation email section to get a HTML email for confirmation, the email works but it doesn't display html, it displays the code. Someone please let me know what I need to fix here?
I suspect its something to do with the headers.
I haven't done much PHP in a while.
<?php
$toEmail = "my#gmail.com";
if(isset($_POST['txtfirstname']))
{
$txtfirstname=$_POST['txtfirstname'];
$txtlastname=$_POST['txtlastname'];
$txtcompanyname=$_POST['txtcompanyname'];
$txttitle=$_POST['txttitle'];
$txtemail=$_POST['txtemail'];
$txtphone=$_POST['txtphone'];
$txtaddress=$_POST['txtaddress'];
$txtcity=$_POST['txtcity'];
$txtstate=$_POST['txtstate'];
$txtzipcode=$_POST['txtzipcode'];
$txtcountry=$_POST['txtcountry'];
$txtshirtsize=$_POST['txtshirtsize'];
$mimeHeaders .= "MIME-Version: 1.0\r\n";
$mailHeaders .= "X-Priority: 3\r\n";
$mailHeaders .= "X-Mailer: PHP". phpversion() ."\r\n";
//$mailHeaders .= "Content-Type: text/html; charset=iso-8859-1\n";
//headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers = "From: " . $txtfirstname." ".$txtlastname . "<". $txtemail .">\r\n";
$headers .= "Reply-To: ". $txtfirstname." ".$txtlastname . "<". $txtemail .">\r\n";
$headers .= "Return-Path: ". $txtfirstname." ".$txtlastname . "<". $txtemail .">\r\n";
$subject = "New Registration";
$content = "New Registration \r\n";
$content = $content. "First Name : ".$txtfirstname. "\r\n";
$content = $content. "Last Name : ".$txtlastname . "\r\n";
$content = $content. "Company : ".$txtcompanyname . "\r\n";
$content = $content. "Title : ".$txttitle . "\r\n";
$content = $content. "Email : ".$txtemail . "\r\n";
$content = $content. "Phone : ".$txtphone ."\r\n";
$content = $content. "City : ".$txtcity . "\r\n";
$content = $content. "State : ".$txtstate . "\r\n";
$content = $content. "Zip Code : ".$txtzipcode . "\r\n";
$content = $content. "Country : ".$txtcountry . "\r\n";
// Subject of confirmation email.
$conf_subject = 'Confirmation Emailer';
// Who should the confirmation email be from?
$conf_sender = 'Me <mygmail.com>';
// HTML email message
$conf_message = '
<html>
<head>
<title>HTML email</title>
</head>
<body>
<table border="0" style="width: 500px; border-collapse: collapse; margin-left: auto; margin-right: auto;" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="width: 100%;"><img src="/banner.jpg" alt="Title" /></td> </tr> <tr> <td style="width: 100%;"><p><span><strong>Congratulations!</strong></span></p>
<p> </p>
<p>You have successfully registered ’</p> <p> </p> <p>Click below to add this event to your calendar-</p>
<p><br /><u><strong>Add to your calendar</strong></u></p>
<p><br />If you require any further assistance or have any queries, please reach out to us at <b>me#<wbr />gmail.com</b></p>
<p><br />We’re looking forward to your presence at the event.</p>
<p><br />23 th December 2020 | 8:45 AM .</p>
</td> </tr>
</tbody> </table>
</body>
</html>
';
mail( $_POST['txtemail'], $conf_subject, $conf_message,'From: ' . $conf_sender );
if(isset($_POST['chkgiveaway']) && $_POST['chkgiveaway']=="Yes")
{
$content = $content. "Want to receive Give aways: ".$_POST['chkgiveaway']. "\r\n";
$content = $content. "Shirt Size: ".$_POST['txtshirtsize']. "\r\n";
$content = $content. "Mailing Address : " . "\r\n";
$content = $content. "Address : ".$_POST['txtaddressgiveaway'] . "\r\n";
$content = $content. "City : ".$_POST['txtcitygiveaway'] . "\r\n";
$content = $content. "State : ".$_POST['txtstategiveaway'] . "\r\n";
$content = $content. "Zip Code : ".$_POST['txtzipcodegiveaway'] . "\r\n";
$content = $content. "Country : ".$_POST['txtcountrygiveaway'] . "\r\n";
}
if(mail($toEmail, $subject, $content, $mailHeaders)) {
$message = "<h4>Thank you, your details have been successfully submitted.</h4>";
echo $message;
}
} else {
echo "Invalid submission";
}
?>
You didn't add the headers
mail( $_POST['txtemail'], $conf_subject, $conf_message, $headers);

generating a html email in php on submit

i am trying to figure out writing html in php file. it seems that php does not recognise all the html tag like font, img. it gave an error. I tried echo 'html stuff'; but it dont seem to work as well.
I have left out the other $var declaration in the code below. the script is working except when i tried to add font size or img tags.
<?php
header("Content-type: html");
$message = "
<!DOCTYPE html>
<html>
<head>
<title>New Loan Enquiry</title>
</head>
<body>
<h2><strong>Time of Enquiry: $today</strong></h2>
Name: $name<br>
Email: $email<br>
Contact: $contact<br>
Buy_Stage: $buystage<br>
Property Type: $pty_type<br>
Property Stage: $pty_stage<br>
Purchase Price: $purchaseprice<br>
Loan Amount: $loanamt<br>
Rate Type: $rate_type<br>
Comments: $comments<br><br>
</body>
</html>
";
mail($to,$subject,$message,$headers);
?>
where did i go wrong? It recognize h2 but not h1 or h3.
do i have to do like
echo ' html';
for each html code line?
Please check mail() in documentation.
To send email with html content you need to do this way:
<?php
$to = 'aidan#example.com';
// subject
$subject = 'subject of email';
// message
$message = 'some html content...';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
and you don't need to use:
header("Content-type: html");
you need to add
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <from#example.com>' . "\r\n";
$headers .= 'Cc: cc#example.com' . "\r\n";
and remove
header("Content-type: html");
I find it best to style Html email in php by using tables and inline styles like below. here is a link to reference. Link, Sadly internal and external style sheets don't always work across different email clients.
<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$message = '<html>';
$message .= '<head>';
$message .= '<title>New Loan Enquiry</title>';
$message .= '</head>';
$message .= '<body>';
$message .= '<h2><strong>Time of Enquiry: $today</strong></h2>';
$message .= '<img src="http://YOUR_IMAGE_URL"/>;
$message .= '<table width="auto" border="0" cellspacing="3px" cellpadding="0">';
$message .= '<tr><td><strong>Name:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$name</td></tr>';
$message .= '<tr><td><strong>Email:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$email</td></tr>';
$message .= '<tr><td><strong>Contact:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$contact</td></tr>';
$message .= '<tr><td><strong>Buy_Stage:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$buystage</td></tr>';
$message .= '<tr><td><strong>Property Type:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$pty_type</td></tr>';
$message .= '<tr><td><strong>Property Stage:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$pty_stage</td></tr>';
$message .= '<tr><td><strong>Purchase Price:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$purchaseprice</td></tr>';
$message .= '<tr><td><strong>Loan Amount:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$loanamt</td></tr>';
$message .= '<tr><td><strong>Rate Type:</strong></td>';
$message .= '<td>\$rate_type</td></tr>';
$message .= '<tr><td><strong>Comments:</strong></td>';
$message .= '<td style=" font-weight:300 color:#CC0000">\$comments</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
mail($to,$subject,$message,$headers);
?>

How to send values from html form to mail in table PHP

This what I have I tried to put it in a table just like:
<table>
<tr><td>$_POST['onderwerp']</td></tr>
</table>
This is what I have it sends the mail but it's to messy:
<?php
$to = 'example#gmail.com';
$subject = 'Vraag via de website';
$message = 'Onderwerp:'. $_POST['onderwerp'].'<br /><br />'.$_POST['vraag'].'<br /><br />'.'Telefoonummer:'. $_POST['tel'].'<br /><br />'.'Email:'. $_POST['email'] ;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:<eexample#gmail.com>' . "\r\n";
$headers .= 'The shop<example#gmail.com>' . "\r\n";
mail($to, $subject, $message, $headers);
header('Location: contact.html');
?>
I just want to send the variables in a table so that I don't have to search through all the text.
<?php
$to = 'user#example.com';
$subject = 'Vraag via de website';
$msg = "<html>
<head>
<title>Title of email</title>
</head>
<body>
<table cellspacing=\"4\" cellpadding=\"4\" border=\"1\" align=\"center\">
<tr>
<td align=\"center\">Onderwerp</td>
<td align=\"center\"> vraag</td>
<td align=\"center\">Telefoonummer</td>
<td align=\"center\">Email</td>
</tr>
<tr>
<td align=\"center\">".$_POST['onderwerp']."</td>
<td align=\"center\">".$_POST['vraag']."</td>
<td align=\"center\">".$_POST['tel']."</td>
<td align=\"center\">".$_POST['email']."</td>
</tr>
</table>
</body>
</html>";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Site Name <me#mysite.com>' . "\r\n";
mail($to, $subject, $msg, $headers);
?>
you could try this using your variables.
$var = 'test';
$var2 = 'test2';
echo '<table border="1">';
echo '<tr><td>' . $var . '</td></tr>';
echo '<tr><td>' . $var . '</td></tr>';
echo '</table>';
this will show the variables in a table, then you can edit the table using css how you want. :)
I suggest that you include swiftmailer.
Swiftmailer makes sure emails get delivered in the Inbox and you can easily include HTML markup in your emails:
Swiftmailer HTML in email
Just download the Swiftmailer Library, include and configure it like this example:
Sending an email in swiftmailer
Let me know if this helps you out!

Sending HTML Email From PHP - Results Erratic

I designed an HTML page and then converted it to use in PHP in order to send an HTML email.
$message = '<!DOCTYPE html>';
$message .= '<html>';
$message .= '<body bgcolor="#E8E8E8 ">';
$message .= '<table bgcolor="white" >';
$message .= '<tr>';
$message .= '<td style="font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;">';
$message .= '<img src="#" width="200px">';
$message .= 'This is a test page.';
$message .= '</td>';
$message .= '</tr>';
$message .= '</table>';
$message .= '</body>';
$message .= '</html>';
$to = "you#example.com";
$subject = "Pulling my hair out";
$headers = "From: me#example.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);
Even though it looks perfect as a stand-alone html page (and I even made a test php page that echoes the $message array, and it still looks perfect) it will have weird things wrong with it in the email (after it's sent).
Sometimes there will be a random ! in the middle of the text. Sometimes the styling in a tag will not show up in the email (when I 'inspect' the html of the email). It seems erratic.
What am I missing here?
You can make one page emailresetTemplate.php
In this page write these line's
<?php ob_start();?>
//do your html stuff here ....... Example Below..........
<div style="width:698px; margin:0 auto; position:relative;">
<div>
<div style="background:url(<?php echo ABSOLUTE_PATH; ?>images/email/restpass/header.png) no-repeat; width:680px; height:127px; margin:0 0 0 10px;"></div>
</div>
<?php
$contents = ob_get_contents();
ob_clean();
include("emailresetTemplate.php");
$to = $email;
$subject = 'Your Password Reset Request';
$message = $contents;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: email#example.com' . "\r\n" .
'Reply-To: email#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(#mail($to, $subject, $message, $headers)){
print "An email containing the password has been sent to you at " . $row["eMail"];
} else {
echo("No such login in the system. please try again.");
}
?>
</div>

styling emails that come in from a php form

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

Categories