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.
Related
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);
?>
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...";
}
?>
I am trying to embed the image to the html content of email in php but whenever the email sent it show me broken link of image there. Image is not embeding to the email here is the code
<?php
$to = 'xxxx#xxxx.com';
$subject = "Beautiful HTML Email using PHP by CodexWorld";
$htmlContent = '
<html>
<head>
<title>Welcome to CodexWorld</title>
</head>
<body>
<h1>Thanks you for joining with us!</h1>
<table cellspacing="0" style="border: 2px dashed #FB4314; width: 300px; height: 200px;">
<tr>
<th>Name:</th><td>CodexWorld</td>
</tr>
<tr style="background-color: #e0e0e0;">
<th>Email:</th><td>contact#codexworld.com</td>
</tr>
<tr>
<th>Website:</th><td>www.codexworld.com</td>
</tr>
<tr>
<th>image:</th><td><img src="http://xxxxxxxxxxxxx.com/email/1.png"/></td>
</tr>
</table>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: xxxx#xxxxx.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Please guide me where is I am wrong
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();
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!