Problems sending html email in php - php

I'm trying to send an email to myself that has a layout and images. What I'm I doing wrong?
<?php
$message = $_POST['message'];
$emailsubject = 'site.com';
$webMaster = 'email#site.com';
$body = "
<html>
<body bgcolor=\"e7e7e7\">
<style type=\"text/css\">
#body {margin: auto;border: 0;padding: 0;font-family: Georgia, 'Times New Roman', Times, serif;font-size: 12px;}
#emailHeader {width: 500px;height: 131px;background: url(http://www.site.com/images/image.gif) no-repeat;}
#emailContent {width: 500px;background: url(http://www.site.com/images/image2.gif) repeat-y;text-align: left;padding: 0 33px 0 6px;}
#emailFooter {width: 500px;height: 34px;background: url(http://www.site.com/images/image3.gif) no-repeat;}
</style>
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td valign=\"top\" align=\"center\">
<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td id=\"emailHeader\"></td>
</tr>
<tr>
<td id=\"emailContent\">
content $message
</td>
</tr>
<tr>
<td id=\"emailFooter\"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>"
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailsubject, $body, $headers);
if ($success) {
echo "Your message was sent.";
}
else{
echo "There was a error.";
}
?>

You should use phpmailer instead of PHP's mail()-Function. It allows you to easily send HTML-Mails.
Besides that you can try to validate your HTML-Code to be compatible for emailing.
Best wishes,
Fabian

You have an error in your code:
WRONG
$headers .= "Content-type: text/html\r\n";
RIGHT
$headers = "Content-type: text/html\r\n";
The .= throws a parse error in PHP unless you previously set $headers somewhere else.
It also may depend on the email client you are testing with. Be sure to check out http://www.email-standards.org/ to check what your email client supports.

You may also want to look into Zend_Mail from Zend Framework:
http://framework.zend.com/manual/en/zend.mail.html
Would make dealing with headers, formats, MIME, etc. easier.

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);
?>

image is not embeding in email in php

I am trying to embed the image to the html content of email in php but whenever the email sent it show me broken link of image there. Image is not embeding to the email here is the code
<?php
$to = 'xxxx#xxxx.com';
$subject = "Beautiful HTML Email using PHP by CodexWorld";
$htmlContent = '
<html>
<head>
<title>Welcome to CodexWorld</title>
</head>
<body>
<h1>Thanks you for joining with us!</h1>
<table cellspacing="0" style="border: 2px dashed #FB4314; width: 300px; height: 200px;">
<tr>
<th>Name:</th><td>CodexWorld</td>
</tr>
<tr style="background-color: #e0e0e0;">
<th>Email:</th><td>contact#codexworld.com</td>
</tr>
<tr>
<th>Website:</th><td>www.codexworld.com</td>
</tr>
<tr>
<th>image:</th><td><img src="http://xxxxxxxxxxxxx.com/email/1.png"/></td>
</tr>
</table>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: xxxx#xxxxx.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Please guide me where is I am wrong

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

sending html email using php / outlook not displaying html properly

i am using php to send a html email like so in my send_email.php file:
<?php
// multiple recipients
$to = 'mark.obrien2014#inbox.com';
// subject
$subject = 'Hewden New Supplier Setup';
// message
$message = file_get_contents('../../assets/email/email.php');
// 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: Mark <mark.obrien2014#inbox.com>' . "\r\n";
$headers .= 'From: Hewden New Supplier Setup <NewSuppliers#hewden.co.uk>' . "\r\n";
$headers .= 'Cc: purchasing#hewden.co.uk' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I am including my email html in a seperate file email.php using
$message = file_get_contents('../../assets/email/email.php');
I am testing my email has been sent using Outlook 2010 on windows and also testing it in a normal webmail. The email sends fine on both but in outlook the html doesn't seem to display properly. My email looks fine in every other email program and all other webmail. can someone please show me what i am doing wrong? thanks in advance
my email html:
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body leftpadding="0" offset="0" paddingheight="0" paddingwidth="0" toppadding="0" style="padding-top: 0; padding-bottom: 0; padding-top: 0; padding-bottom: 0; background:#F6F6F6; width: 100% !important; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; -webkit-font-smoothing: antialiased;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table align="center" class='bgBody' border="0" cellpadding="0" cellspacing="0" style="font-family:helvetica, sans-serif; background:#fff; border:1px solid #999; margin-top:50px;" width="750">
<tr>
<td align="center">
<img src="http://hewdenportal.co.uk/assets/email/images/bigImg.png" width="750" height="258" />
</td>
</tr>
</table>
</tr>
</table>
<table align="center" class='bgBody' border="0" cellpadding="0" cellspacing="0" style="font-family:helvetica, sans-serif; font-weight:100; font-size:16px; background:#000; color:#4f4f4f; border:1px solid #999; margin-top:20px;" width="752">
<tr>
<td align="left">
<div class="container" style="min-height:200px; width:690; background:#fff; padding-left:30px; padding-right:30px; padding-top:20px; padding-bottom:20px; font-family:helvetica, sans-serif; font-weight:100; font-size:15px;">
<div class="text" style="width:72%;">
<h1 style="font-family:helvetica, sans-serif; font-weight:100; font-size:20px;">Getting Started</h1>
<p>Thanks for taking an Interest in joining Hewden as an approved supplier.<br/>We care about providing a good quality to service to Customer's and Supplier's alike.</p>
<p>As part of the Hewden journey blah blah blah.</p>
</div>
<div class="find_more" style="width:110px; border:1px solid #666; height:20px; background:rgba(255,195,82,1); margin-top:10px; float:right; position:relative; cursor:pointer; cursor:hand; text-align:center; padding:6px;">Find out More ></div>
</div>
</td>
</tr>
</table>
</body>
</html>
email in webmail showing correct html:
email in outlook with distorted html:
Use or <button> tags instead of a <div> tag.
As far as I know (I send HTML emails pretty often), buttons are well more accepted on Outlook and other email providers.
HTML emails are very annoying sometimes... :)
Using div in html emails is a little tricky and if you can get away with table that'd be easier in general. I would guess the specific issue here is that Outlook 2010 doesn't support width, position, or float for div. Campaign Monitor wrote a nice roundup of div compatibility here.

Effect of using PHP mail()

I've got a project where by I send email using PHP's inbuilt mail() function, I'm only sending one email at a time with a small amount of HTML and very limited CSS (two tables and a little CSS in the head for styling), but the server seems to be doing it really slowly (so much so that the page upon which an admin sends the email frequently times out)
So my question is this; does mail() put a high workload on the server (not sure if that is the right term) or is it just that the server I'm using is rubbish?
Is it worth me looking into projects like http://pear.php.net/package/Mail for this kind of thing?
EDIT:
Here is the code in question:
$query = "SELECT email FROM $a_table WHERE id='$Id'";
$result = mysql_query($query) or die("Query failed: ".mysql_error());
$mail_to = mysql_fetch_row($result);
$mail_to = $mail_to[0];
// multiple recipients
$to = $mail_to;
// subject
$subject = 'notification';
// message
$message = '<html>
<head>
<title>title goes here</title>
<style type="text/css">
table { border: 1px solid #000;}
table tr th { background-color: #d8d8d8; border-bottom: 1px solid #000}
table tr th, table tr td { padding: 4px; text-align: center; }
</style>
</head>
<body>
<h1>header goes here</h1>
<table cellspacing="0">
<tr>
<th>th1</th><td>'.$var.'</td>
</tr>
<tr>
<th>th2</th><td>'.$var2.'</td>
</tr>
<tr>
<th>th3</th><td>'.$var3.'</td>
</tr>
</table>
<p> </p>
<table cellspacing="0">
<tr>
<th colspan="13">Key</th>
</tr>
<tr>
<th>G</th>
<th>I</th>
<th>L</th>
<th>M</th>
<th>N</th>
<th>O</th>
<th>Q</th>
<th>R</th>
<th>S</th>
<th>V</th>
<th>W</th>
<th>C</th>
<th>?</th>
</tr>
<tr>
<td>G</td>
<td>I</td>
<td>L</td>
<td>M</td>
<td>U</td>
<td>O</td>
<td>Q</td>
<td>R</td>
<td>S</td>
<td>V</td>
<td>W</td>
<td>C</td>
<td>R</td>
</tr>
</table>
</body>
</html>
// 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 .= 'From: admin<admin#admin.com>' . "\r\n"; // might need to get rid of this soon
// Mail it
mail($to, $subject, $message, $headers);
}
The mail() function is usually very fast. I've used it in the past for mass email systems and that was processing hundreds of emails per second.
I'd recommend checking how your system is configured to send email. mail() generally utilizes your system's sendmail install (or postfix). You should think about checking the logs to see if there is an problem there.

Categories