Gmail not showing images in email sent using php - php

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

Related

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 HTML-formatted message with standard mail function in PHP

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

How to use html in a php email [duplicate]

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

Mail generated using PHP mail function showing only code

I have written a code to send mail using PHP mail() function
$message = "
<html>
<head>
<title>TITLE</title>
</head>
<body>
<p>Customer ".$customerName." with id ".$customerId." wishes to place order for the following products that are currently note available in stock</p>
<table>
<tr>
<th align='center'>Product Name</th>
<th align='center'>Product Id</th>
<th align='center'>Product Part Number</th>
<th align='center'>Quantity</th>
</tr>"
.$content.
"
</table>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: '.$customerEmail.'' . "\r\n";
mail($to,$subject,$message,$headers);
The mail is working fine for me, but for the client it appears like this
From:
Sent: None
To: orders#xxxxx.com
Subject: SUBJECT
Content-type:text/html;charset=UTF-8
From: xxxx#xxxx.com
Message-Id: <20140908144611.165ACC36BC#SV08.xxxxxx.com>
Date: Mon, 8 Sep 2014 17:46:11 +0300 (AST)
X-Nonspam: None
X-Antivirus: avast! (VPS xxx-0, 09/09/2014), Inbound message
X-Antivirus-Status: Clean
<html>
<head>
<title>TITLE</title>
</head>
<body>
<p>Customer XXXX with id 3
wishes to place order for the following products that are currently not
available in stock</p>
<table>
<tr>
<th align='center'>Product Name</th>
<th align='center'>Product Id</th>
<th align='center'>Product Part Number</th>
<th align='center'>Quantity</th>
</tr><tr><td
align='center'>SCREW;BHHS;3/8-16 X .75</td><td align='center'>3237<td
align='center'>35059</td><td align='center'>6</td></tr>
</table>
</body>
</html>
Im not understanding why its working for our tests but not for the client. I tested using my gmail account and it shows fine where as the client is using his mail id on his company domain.
Instead of php mail function, I suggest you to use the phpMailer library with google mail smtp credentials. this will help you to send HTML template mails.
Reference URL - PHP mailer example url
Use the following header lines
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Remove and tags from your $message. And change headers to
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

how to remove the "Via" mail in sending a mail [duplicate]

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

Categories