Sending html with email body - php

I am trying to insert and send email with html body but the received email is pure html text.I've added correct headers but seems that they not helping me ( or at least I think are correct one ).
Here is the code
if(isset($_POST['email'])) {
$email_to = "";
$email_subject = "";
$firstname = $_POST['name']; // required
$email = $_POST['email']; // required
$message = $_POST['message']; // not required
$phone = $_POST['phone'];
$captcha = $_POST['captcha'];
$subject = $_POST['subject']; // required
$email_message = "New email:\n\n";
$email_message .= '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="top" bgcolor="#838383" style="background-color:#838383;"><br>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top"><img src="images/top.png" width="600" height="177" style="display:block;"></td>
</tr>
<tr>
<td align="center" valign="top" bgcolor="#d3be6c" style="background-color:#d3be6c; font-family:Arial, Helvetica, sans-serif; font-size:13px; color:#000000; padding:0px 15px 10px 15px;">
<div style="font-size:24px; color:#555100;"><br>
'.$firstname.'
</div><br>
<div>'.$subject.'</div>
<div>
'.$message.'
'.$email.'
'.$phone.'
</div></td>
</tr>
<tr>
<td align="left" valign="top"><img src="images/bot.png" width="600" height="18" style="display:block;"></td>
</tr>
</table>
<br>
<br></td>
</tr>
</table>
</body>
</html>';
// create email headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
}
http://pastebin.com/5aQh0Gvz

You were resetting $headers when setting the From: header:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n". // << note the dot before = that wasn't here before making `$headers` to be resetted
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();

Related

PHP mail function sending the email, but not the html form data

The HTML consists of simple input fields as listed in the php below, what happens is PHP mail() sends the email but excludes the form data itself. I have spent a lot of time but not sure why this is happening.
and the php to send the mail:
<?php
$to = "user#gmail.com";
$subject = "Question Bank - New Question";
$message = '<html>
<head>
<title>New Question</title>
<style>
table {
border: 1px solid black;
}
table th, tr{
text-align:center;
}
</style>
</head>
<body>
<table cellspacing="20px">
<tr>
<th>Username</th>
<th>Email</th>
<th>Question</th>
<th>Question Hint</th>
<th>Correct Answer</th>
</tr>
<tr>
<td>'.$_POST["username"].'</td>
<td>'.$_POST["email"].'</td>
<td>'.$_POST["question"].'</td>
<td>'.$_POST["questionhint"].'</td>
<td>'.$_POST["answer"].'</td>
</tr>
</table>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: user#company.com' . "\r\n";
$headers .= 'Cc: cc#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

Send HTML links in php mail message

I am trying to send a html link in mail using php. But I am getting the full text of html in the mail. Below is the code what I have tried . But it is not working. Any help is appreciated.
$emailid = $_POST['email'];
$phoneno = $_POST['phoneno'];
$to = "demoemail#gmail.com"; // this is your Email address
$from = $emailid; // this is the sender's Email address
$subject = "Intrested in scheduling appointment.";
$message = "<a href='http://www.google.com'>Click Here</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From:" . $from;
$retval = mail($to,$subject,$message,$headers);
Below is what I am getting in mail with tag
http://www.google.com'> Click Here
I've checked with your code..
Seems like you are making a simple mistake..
You are not concatenating your code.. you are re assigning your header at line 3
Your Code
$emailid = $_POST['email'];
$phoneno = $_POST['phoneno'];
$to = "demoemail#gmail.com"; // this is your Email address
$from = $emailid; // this is the sender's Email address
$subject = "Intrested in scheduling appointment.";
$message = "<a href='http://www.google.com'>Click Here</a>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From:" . $from;
$retval = mail($to,$subject,$message,$headers);
Updated Code on headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from;
Update your header and try again!
Thanks! Have a nice day!
Working code with template paste and run
<?
$to = $send_to;
$subject = $mail_subject;
$message .= '<div style="margin:0;font-weight:lighter;background:#f0f4f7;">
<table class="m_6501530908114331344layout" style="font-family:Helvetica,Arial,sans-serif;background:#f0f4f7;width:100%">
<tbody><tr>
<td>
<table cellspacing="0" class="m_6501530908114331344page-container" width="505" align="middle" style="font-family:Helvetica,Arial,sans-serif;border-spacing:0;background-color:#fff;max-width:505px!important;display:block!important;margin:25px auto!important;clear:both!important">
<tbody><tr class="m_6501530908114331344header" style="background-color:rgba(218, 23, 23, 0.03);">
<td class="m_6501530908114331344row" style="border-left:1px solid #e7e7e7;border-right:1px solid #e7e7e7;border:none;padding:20px 35px">
<img alt="lasaioffice" height="80" width="300" src="https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png" class="CToWUd">
</td>
</tr>
<tr class="m_6501530908114331344subject" style="background-color:#fff">
<td class="m_6501530908114331344primary-message" style="padding:20px 35px 0">
<p style="margin:20px 0;line-height:1.45">Hi google </p>
<h2 class="m_6501530908114331344centered" style="font-size:24px;font-weight:bold;margin:0;margin-bottom:30px;display:block;text-align:center;margin-bottom:0;color:#eb2028;">
Just one more click
</h2>
<table class="m_6501530908114331344email-btn" style="font-family:Helvetica,Arial,sans-serif;background:#eb2028;padding:10px">
<tbody><tr>
<td>
Activate Account
</td>
</tr>
</tbody>
</table>
<p style="margin:20px 0;line-height:1.45">
<strong style="font-weight:bold">
Kind Regards, <br>
The google Team
</strong>
</p>
</td>
</tr>
<tr class="m_6501530908114331344footer" style="color:#7f8da0">
<td class="m_6501530908114331344footer-tagline" style="padding:0 35px">
<hr class="m_6501530908114331344footer-line-break" style="margin-bottom:35px;border:0;border-bottom:1px solid #d3dbe3">
<p class="m_6501530908114331344small" style="font-size:12px">
<strong style="font-weight:bold">
<a style="color:#eb2028;" href="http://google.com">google</a> -
The leading marketplace selling google.
</strong>
</p>
<p class="m_6501530908114331344small" style="font-size:12px">
This account notification was sent to you because you are a google user.
Should you have any questions, you are welcome to email us.
</p>
</td>
</tr>
<tr class="m_6501530908114331344footer" style="color:#7f8da0">
<td class="m_6501530908114331344footer-links" style="padding:20px 35px 45px">
<p class="m_6501530908114331344small" style="font-size:12px">
Copyright © 2017 | google Pty Ltd. All rights reserved.
</p>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
<img src="" alt="" width="1" height="1" border="0" style="height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="CToWUd"><div class="yj6qo"></div><div class="adL">
</div></div>';
$header = "From:abc#gmail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>

Sending email with PHP, Gmail doesn't work

When I try to send my mail via php, GMail does not accept my html:
Simple example:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body style="padding:0px; margin:0PX;" bgcolor="#dfdfdf">
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" bgcolor="" style="table-layout:fixed; margin:0 auto;">
<tr>
<td width="640" align="center" valign="top">
Screenshot: <img src="data:image/jpeg;base64,someimginfo" />
</td>
</tr>
</table>
</body>
</html>
GMail output:
Gmail edits my text e.g. <html> --> "<html>"
The headers I use: MIME-Version: 1.0 & Content-type: text/html; charset=UTF-8
What am I doing wrong?
EDIT PHP Code:
<?php
$recipient = "xxxxxx#gmail.com";
$image = $_POST["image"];
$contact = $_POST["contact"];
$token = $_POST["token"];
if($token != "***"){
exit('{"success":false, "error":"Invalid Token"}');
}
$from = (filter_var($contact, FILTER_VALIDATE_EMAIL)) ? $contact : 'no- reply#example.com';
$header = 'From: webmaster#example.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$txt = '
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body style="padding:0px; margin:0PX;" bgcolor="#dfdfdf">
<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" bgcolor="" style="table-layout:fixed; margin:0 auto;">
<tr>
<td width="640" align="center" valign="top">
Screenshot: <img src="data:image/jpeg;base64,' . $image . '" />
</td>
</tr>
</table>
</body>
</html>
';
if(mail($recipient, "APP Support request", $txt, $header)){
exit('{"success":true}');
}
exit('{"success":false, "image":"' . $image . '"}');
?>
PHPmailer is the best class for the job!
https://github.com/PHPMailer/PHPMailer
Edit: replaced http://phpmailer.worxware.com/ with the github address.
It looks like the problem is in these lines:
$header = 'From: webmaster#example.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
// …
if(mail($recipient, "APP Support request", $txt, $header)){
You switch between the names $header and $headers. So when you add the text/html content type to the variable $headers, that isn’t affecting the $header variable that is being used to actually send the email.
You should switch all those variables to the same name, $headers.

How to send mail using php by insert html into mail content? [duplicate]

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

php email from name header based on user name input

What am I doing wrong here. When a user submits the form, it shows up in my inbox with their name as their email address and I would like it to be the name that they input in the form. here is my code.
<?php
//error_reporting(0);
//include("connection.php");
$n=$_POST['name'];
$e=$_POST['email'];
$p=$_POST['phone'];
$t1=$_POST['tot_eth'] ;
$t2=$_POST['tot_tax'];
$t3=$_POST['tot_acct'];
$t4=$_POST['tot_tot'];
$i=1;
while($i<13)
{
${'date' . $i} = $_POST["date$i"] ;
${'seminar'.$i}=$_POST["seminar$i"] ;
${'sponser'.$i}=$_POST["sponser$i"] ;
${'ethics'.$i}=$_POST["ethics$i"] ;
${'tax'.$i}=$_POST["tax$i"] ;
${'acct'.$i}=$_POST["acct$i"] ;
${'t'.$i}=$_POST["total$i"] ;
$i++;
}
//session_start();
$to = "mj#ntatax.com";
$from=$n $e;
$subject = "2015 CPE Verification Form";
// compose headers
$headers = 'From: '. $_POST['name'] . ' <' . $e . '>\r\n' .
'Reply-To:' .$from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// compose message
$message ='<html>
<body>
<p>
From Name : '.$n.'<br>
From Email : '.$e.'<br>
Phone Number : '.$p.'<br><br>
</p>
<table cellpadding="5"> <tr>
<td>Date </td>
<td>Seminar Attended and Location </td>
<td>Sponsor </td>
<td>Ethics Hours </td>
<td>Tax Hours </td>
<td>Acct Hours</td>
<td>Total Hours</td>
</tr>
';
for ($i=1;$i<=13;$i++){
$message .='<tr>
<td>'.${'date' . $i}.'</td>
<td>'.${'seminar'.$i}.'</td>
<td>'.${'sponser'.$i}.'</td>
<td>'.${'ethics'.$i}.'</td>
<td>'.${'tax'.$i}.'</td>
<td>'.${'acct'.$i}.'</td>
<td>'.${'t'.$i}.'</td>
</tr>';
}
$message .=' <tr></tr>
<tr>
<td colspan=2>
<td><p style="text-align: right">Total</p></td>
<td>'.$_POST['tot_eth'].'</td>
<td>'.$_POST['tot_tax'].'</td>
<td >'.$_POST['tot_acct'].'</td>
<td >'.$_POST['tot_tot'].'</td> </td>
</tr> </table>
</body>
</html>';
$message = wordwrap($message, 270);
// send email
mail($to,$subject, $message, $headers,"'".'-f '.$from."'");
// mail($to,$subject, $message, $headers,"'".'-f '.$from."'");
session_start();
//$_SESSION['send1']="Thanks For Contacting Us,Your Query Will Be Replied At the Earliest.";
header("location:http://www.waainc.org");
?>

Categories