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);
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
How to send mail using php by insert html into mail content ?
I tried to insert html code inner $message, When i test it's show error
like this Parse error: syntax error, unexpected 'margin' (T_STRING)
How can i do ?
<?PHP
include("connect.php");
$email = "test_mail#hotmail.com";
$to = $email;
$subject = "test subject";
$message = "
<body style="margin: 0; padding: 0;">
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<img src="http://i.stack.imgur.com/Jy9QUm.jpg"/>
</td>
</tr>
<tr>
<td>
test text
</td>
</tr>
</table>
</body>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>
You have an issue with wrapping your $body string try this
<?PHP
include("connect.php");
$email = "test_mail#hotmail.com";
$to = $email;
$subject = "test subject";
$message = "
<body style='margin: 0; padding: 0;'>
<table border='1' cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td>
<img src='http://i.stack.imgur.com/Jy9QUm.jpg'/>
</td>
</tr>
<tr>
<td>
test text
</td>
</tr>
</table>
</body>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>
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);
?>
This question already has answers here:
php send e-mail with PDF attachment
(4 answers)
Closed 7 years ago.
Good day! My code is working great if the format is in msword but when i changed it to PDF it becomes corrupted what should i do? Please help me.
$headers = "From:<noreply#example.com.ph>";
$to = 'example#example.com';
$subject = 'Purchase Order';
$message .= 'Please see attached file';
$txt .=" <html> <body>
<p><b> PO Number:</b>
$purchasenumber</p>
<p><b> Style Code:</b> $styleCode</p>
<p><b> Generic Number:</b> $gennum</p>
<p><b> Vendor Name:</b> $vendname</p>
<p><b> Planned Delivery Date:</b>
$pdelivdate</p> <br/> <br/>
<table border=1 style='width:100%' cellpadding='0'>
<thead>
<tr>
<th width='16.7%'>Material Number</th>
<th width='16.7%'>Color</th>
<th width='16.7%'>Size</th>
<th width='16.7%'>Ordered QTY</th>
<th width='16.7%'>Total Cost</th>
<th width='16.7%'>Total SRP</th>
</tr>
</thead>
<tbody>
";
$statement = $db->prepare("SELECT * FROM purchaseorderproductitem where purchaseorderID = :pid");
$statement->execute(array(':pid' => $purchasenumber));
foreach ($statement->fetchAll() as $row)
{ $matnum = $row['materialnumber']; $color = $row['color']; $size = $row['size']; $qty = $row['quantity']; $units = $row['units']; $curcost = $qty * $cost; $cursrp = $qty * $srp; $curcost = number_format($curcost, 2, '.', ''); $cursrp = number_format($cursrp, 2, '.', '');
$txt .="
<tr> <td width='16.7%'>$matnum</td> <td width='16.7%'>$color</td> <td width='16.7%'>$size</td> <td width='16.7%'>$qty $units</td> <td width='16.7%'>$curcost</td> <td width='16.7%'>$cursrp</td> </tr>
";
}
$txt .="
<tr> <td width='16.7%' text-align:'center'>Total</td> <td width='16.7%'> </td> <td width='16.7%'> </td> <td width='16.7%'>$totalqty pcs</td> <td width='16.7%'>$totalcost</td> <td width='16.7%'>$totalsrp </td> </tr>
</body> </table> </html>
";
// Always set content-type when sending HTML email $message = "MIME-Version: 1.0" . "\r\n"; // $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $message .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fileatt_name2 = "PurchaseOrder.pdf";
$semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment $headers .= "\nMIME-Version:
1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $data2 = chunk_split(base64_encode($txt));
$message = "{$mime_boundary}\n" . "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" . "Content-Transfer-Encoding: 7bit\n\n" .
$message .= "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .
// Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . // {$fileatt_type} " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n";
// Send the message $send = mail($to, $subject, $message, $headers);
Can you please help me to solve this issue? Thanks in advance!
You can use a combination of PHPMailer:
http://phpmailer.worxware.com/
And TCPDF:
http://www.tcpdf.org/
To accomplish this task. I will not cover the processes in detail as code examples would be quite tedious to create however both pieces of software have detailed documentation and examples found here:
https://github.com/Synchro/PHPMailer
And here:
http://www.tcpdf.org/docs.php
Edit:
If you do not want to to use something that just works like PHPMailer then I would ensure that the correct headers are being sent.
One useful trick i have found here too is that if you open said corrupted file in a text editor, you most usually find useful information at the very beginning relating to any errors that may have occurred while processing the output.
Edit:
Just guessing here but your last few lines of code I believe should read as follows:
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "boundary=\"{$mime_boundary}\""; $data2 = chunk_split(base64_encode($txt));
$headers .= "{$mime_boundary}\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n";
// Add file attachment to the message
$headers .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . {$fileatt_type} " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n";
// Send the message
$send = mail($to, $subject, $message, $headers);
You had concatenated the end of one of the statements instead of ending it with a semicolon ;
You were adding the headers to the $message variable, they should rather be in the $headers variable correct?
You had added one of the headers twice.
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!
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>