php mail syntax error - php

$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 "/".

Related

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

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.

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!

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

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