I am trying to send a simple HTML e-mail from PHP. The code below simply results in a blank e-mail in GMail. It also has an empty attachment called 'noname', which is not at all what I want; though that might just be a symptom of it not working.
The code I am using is:
<?php
//define the receiver of the email
$to = 'morrillkevin#gmail.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
MIME-Version: 1.0
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b>formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
If possible use the PHPMailer class. It will greatly simplify your work.
It turns out the key is the encoding type. Instead of:
Content-Type: text/plain; charset="iso-8859-1"
I needed to use:
Content-Type: text/plain; charset=us-ascii
It might depend on stuff as detailed as how you save the PHP file in your own text editor. I haven't looked into it, but the iconv function in PHP may have brought me some joy too. So I think this part is really sensitive.
Here is a better snippet of sample code that shows the whole thing end-to-end:
$notice_text = "This is a multi-part message in MIME format.";
$plain_text = "This is a plain text email.\r\nIt is very cool.";
$html_text = "<html><body>This is an <b style='color:purple'>HTML</b> text email.\r\nIt is very cool.</body></html>";
$semi_rand = md5(time());
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$to = "Me <foo#gmail.com>";
$from = "Me.com <me#me.com>";
$subject = "My Email";
$body = "$notice_text
--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
$plain_text
--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
$html_text
--$mime_boundary--";
if (#mail($to, $subject, $body,
"From: " . $from . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative;\n" .
" boundary=" . $mime_boundary_header))
echo "Email sent successfully.";
else
echo "Email NOT sent successfully!";
exit;
-Kevin
You have to specify the mime-type within the headers-parameter of the mail-function. Add this:
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
It's also shown in the PHP-Documentation for the mail-function. See example 4
If using a library is not a problem just use Swift Mailer
There is something important I want to notice about charsets here:
"Content-Type: text/plain; charset = \"UTF-8\";\n"
is right and not only
"Content-Type: text/plain; charset=UTF-8\n"
Hope I could help other people who frustrate while searching the same mistake like me.
Remember also to write only \r for windows and not for Linux-Servers.
And at the end of the header should be an extra blank line:
$headers .= "Content-Type: text/plain; charset = \"UTF-8\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "\n";
Following code is working for me to remove HTML Tags in mail.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";
Hope this helps you in php
$fname=$_POST['FirstName'];
$lname=$_POST['LastName'];
$dob=$_POST['DOB'];
$name=$fname.' '.$lname;
$HTML="
<style>
<!--
.form_tbl{ border:1px solid #bbbbbb; background-color:#eeeeee;}
.form_tbl td{
font-size:12px;
color:#555555;
padding:5px 5px;
}
h3{
font-family:Georgia, Times New Roman, Times, serif;
font-size:12px;
font-weight:bold;
color:#0099CC;
margin:0;
padding:0;
}
.form_tbl td select{ margin:0; padding:0;}
.tbl_brdr{ border-top:1px solid #bbbbbb; border-left:1px solid #bbbbbb;}
.tbl_brdr td{ border-bottom:1px solid #bbbbbb; border-right:1px solid #bbbbbb; padding:5px 5px 5px 10px; }
-->
</style>
<table class='form_tbl' width='650' border='0' cellspacing='0' cellpadding='3'>
<tr>
<td colspan='2'><strong>PERSONAL DETAILS:</strong></td>
</tr>
<tr>
<td width='250' > Name:</td>
<td >".$name."</td>
</tr>
<tr>
<td valign='middle'>Date Of Birth:</td>
<td valign='top'>".$dob."</td>
</tr>
</table>
";
$from=$name;
$subject='Student Registration Form';
$to= 'yourmail#mail.com';
//$to='development#ezone.com.np';
function sendHTMLemail($HTML,$from,$to,$subject)
{
// First we have to build our email headers
// Set out "from" address
$headers = "From: $from\r\n";
// Now we specify our MIME version
$headers .= "MIME-Version: 1.0\r\n";
// Now we attach the HTML version
$headers .= //"--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n";
//"Content-Transfer-Encoding: base64\r\n\r\n";
// And then send the email ....
if (mail($to,$subject,$HTML,$headers))
{
$_SESSION['msg']="Details successfully sent!";
}
else
{
$_SESSION['msg']="Sending Failed! Please try again later.";
}
}
sendHTMLemail($HTML,$from,$to,$subject);
?>
Related
$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 "/".
I am unable to get images in gmail.
Below is my code:
$message2='<html><head></head><body><table width="100%" cellpadding="10" style="border:1px solid #3A5896;" rules="all" ;=""><tbody><tr><td><img alt="PHP Gang" src="http://www.phpgang.com/wp-content/uploads/gang.jpg"></td></tr><tr><td colspan="2">Dear $Name,<br><br>We thank you for subscribe phpgang.com you are now in phpgang download list you can download any source package from our site.</td></tr><tr><td font="colr:#999999;" colspan="2"><i>PHPGang.com<br>Solve your problem. :)</i></td></tr></tbody></table>here</body></html>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1". "\r\n";
$headers .= "From: $from\r\n";
mail($to, $subject, $message2, $headers);
I am trying to use the following in my contact us form to receive nicely formatted html email from my visitors:
$message .='<table width="800" border="0" cellspacing="3" cellpadding="3" style="margin-bottom: 20px">';
$message .='<caption style="text-align:left;background:#eee;padding:5px;font-size:18px;font-weight:bold;">Webmail</caption>';
$message .='<tr><td width="200"><strong>Date</strong></td><td width="600" style="border:1px solid #ccc;">'.$todayis.'</td></tr>';
$message .='<tr><td width="200"><strong>From</strong></td><td width="600" style="border:1px solid #ccc;">'.$name.'</td></tr>';
$message .='<tr><td width="200"><strong>Email</strong></td><td width="600" style="border:1px solid #ccc;">'.$email.'</td></tr>';
$message .='<tr><td width="200"><strong>Subject</strong></td><td width="600" style="border:1px solid #ccc;">'.$recipient.'</td></tr>';
$message .='</table>';
$message .= '<div style="width:770px;background:#eee;padding:15px;">';
$message .= '<div style="font-weight: bold; font-size: 18px;padding-bottom: 10px;">Your Message</div>';
$message .= $message;
$message .= '</div>';
$message .='<table width="800" border="0" cellspacing="3" cellpadding="3" style="margin-bottom: 20px">';
$message .='<caption style="text-align:left;background:#eee;padding:5px;font-size:18px;font-weight:bold;">Webmail Data</caption>';
$message .='<tr><td width="200"><strong>IP Address</strong></td><td width="600" style="border:1px solid #ccc;">'.$ip.'</td></tr>';
$message .='<tr><td width="200"><strong>Browser Info</strong></td><td width="600" style="border:1px solid #ccc;">'.$httpagent.'</td></tr>';
$message .='<tr><td width="200"><strong>Referral</strong></td><td width="600" style="border:1px solid #ccc;">'.$httpref.'</td></tr>';
$message .='</table>';
$headers = "From: " . $email . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 1\r\n";
$sent = mail($to, $subject, $message, $headers);
The problem I have is that when I use text/html the email shows as sent but I am not getting it, when I am changing it to text/plain everything works fine except that I get everything in plain text without any HTML formatting.
I am on my own dedicated Apache server, running Plesk 12.0.18 and CentOS6.6 do I need to configure anything on the server? I have tried to configure the email to receive everything in HTML format, but seems like it did not work.
Any help is appreciated.
Php Html email
<?php
$to = "abc#gmail.com";
$subject = "PUT_SUBJECT_HERE";
$mail_body = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example: ' . $subject . '
<h1>this is a heading</h1>
</body>
</html>';
//$headers = "From: abc#gmail.com";
//$headers .= "Content-type: text/html";
// 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: <abc#gmail.com>' . "\r\n";
mail($to, $subject, $mail_body, $headers);
?>
Enjoy this code
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.
This question already has answers here:
How to remove "via" and server name when sending mails with PHP?
(5 answers)
Closed 8 years ago.
i'm using Cpanel in Hostgator and PHP to send the mail. when i use this code i always receive mail via mail as "birkin.websitewelcome.com" . when i send mail through joomla "via" mail id is not added. i don't want to display "birkin.websitewelcome.com" in the mails.
is there any code to be added in mail.php
mail.php
$guest_ip = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];
$name=mysql_real_escape_string($_GET['name']);
$email=mysql_real_escape_string($_GET['email']);
$subject1=mysql_real_escape_string($_GET['subject']);
$messag=mysql_real_escape_string($_GET['message']);
$to = "id#mydomain.com";
$subject = 'Mail From Contact Page - Surabi Institutions';
$headers = "From: info#mydomain.org \r\n";
$headers .= "Reply-To:info#mydomain.org \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<div style="border: 1px solid #292929; margin:0 auto; height:auto;
width:70%; color:#808080; padding: 0% 10%;">';
$message .= '<h3>Mail From Contact Page</h3>';
$message .= '<strong>Name</strong>:'.$name.'<br>';
$message .= '<strong>Email</strong>:'.$email.'<br>';
$message .= '<strong>Subject</strong>:'.$subject1.'<br>';
$message .= '<strong>Message</strong><br>'.$messag.'<br><br /><br /><br /> </div>';
$message .= '<div style="border-top:1px solid #cacaca; margin-top:50px; height:auto; color:#faa;">';
$message .= '<b>Visitor IP</b> -'.$guest_ip.'<br>';
$message .='<b>Visitor City</b> -'.$guest_city.'<br>';
$message .='</div> ';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "<script type='text/javascript'> alert('Thank you. We will be in touch with you very soon.'); window.location='index.php';</script>";
}
else
{
echo "<script type='text/javascript'> alert('Mail Sending Failed Please Try Again'); history.back();</script> ";
}
you can use this code.
$to = ""test#gmail.com;
$from = "info#test.com";
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
// now lets send the email.
mail($to, $subject, $message, $headers);