generating a html email in php on submit - php

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

Related

PHP mail function fails with "message has lines too long for transport"

I'm using PHP 7.4.30 and I'm totally new to PHP.
$subject ="My subject";
$message = '<html><body>';
$message .= '<div style="text-align: center; width: 100%; background-color: #fff;">';
$message .= '<div class="info"> </div>';
$message .= '<table style="text-align: justify; margin: auto; background-color: #ebebeb; border: 1px solid #e7e7e7; width: 600px;" cellspacing="0" cellpadding="0" bgcolor="#ebebeb" align="center">';
$message .= '<tbody>';
$message .= '<tr style="line-height: 0px;">';
$message .= '<td style="line-height: 0';
$from = "My website<robot#website.com>";
$replyto = "website#website.com";
$headers = 'Content-Type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: '. $replyto . "\r\n";
$headers .= 'Bcc: xxx#xxx.com';
$message = str_replace("\n.", "\n..", $message);
mail($to, $subject, $message, $headers);
Looking at the delivery track from the server, I see the error message:
"message has lines too long for transport"
Can anyone help?
I have recently faced this issue. An easy solution to this issue is replacing this line
$message = str_replace("\n.", "\n..", $message);
with
$message = wordwrap($message, 70,"\r\n");

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

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

Categories