Email adding newlines? - php

I am attempting to send out HMTL emails with php's mail() function and notice that some things are broken in the email. I found out from reading the original in gmail, that lines in the body of the message are all the same length, and this cuts HTML tags in inconvenient places.
My headers are multipart with boundary between html and plain, with 7-bit. An example of what I'm sending is:
Unsubscribe
But because of its position, the message splits it like
<a href="site.co
m/ubsub">unsubscribe</a>
Which breaks the hyperlink. Is there something I'm missing or not getting? I've seen around about using a mail library like swiftmailer--and I can use it--but I want to actually understand this behavior.
EDIT:
How mail() is being called:
$headers = 'From: Me <me#site.com>' . "\r\n" . 'MIME-Version: 1.0'
$headers .= "\r\n" . "Content-Type: multipart/mixed; charset=ISO-8859-1; boundary=$boundary"
$subject = 'subject';
$html_message = '--' . $boundary . "\r\n";
$html_message .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$html_message .= 'Content-Transfer-Encoding: 7bit' . "\r\n" . "\r\n";
$html_message .= $message . "\r\n";
// plaintext added similarly, with different content-type. Omitted.
mail($to,$subject,$html_message, $headers);
EDIT #2:
Expanding on $message.
$message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" /><title>"
$message .= "Page Title</title></head><body style=\"margin: 0; padding: 0; font-family: open \'Open Sans\', \'Open Sans Bold\';\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" ><tr><td><table align=\"center\" bgcolor=\"#FFFFFF\" border=\"0\"cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tr><td align=\"right\" style=\"padding: 20px 20px 20px 20px;\"> <font color=\"#EA5D0F\">[</font> Sign In <font color=\"#EA5D0F\">][</font> About <font color=\"#EA5D0F\">][</font> Search<font color=\"#EA5D0F\">]</font></td></tr><tr><td bgcolor=\"#FFFFFF\" align=\"center\" style=\"padding: 40px 0px 0px 0px;\"><img src=\"http://about.SITE.com/wp-content/uploads/2013/08/SITE.plain_.png\" alt=\"SITE Logo\" width=\"300\" style=\"display: block;\" /></td></tr><tr><td style=\"padding: 10px 20px 20px 20px;\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" ><tr><td bgcolor=\"#FFFFFF\" style=\"padding: 30px 10px 10px 10px; text-align:center; font-size: 1.2em; font-weight:bold;\"> <hr color=\"#EA5D0F\"/>"
This continues for quite a while. Not sure how much to include.

I did have that problem in the past. While i don't know what the cause is, it was solved by replacing every > by >\r\n:
$message = str_replace(">", ">\r\n", $message);

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

How to add button in PHP Mail function?

I'm trying to insert a button in an anchor tag with an href. I want the user to see a button and not a link
<a href='http://example.com/PhpProject3/reset_passwd.php?key="<?= $email; ?>"&userid="<?= $userid; ?>"' style=' background-color: #4CAF50;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;'>Click To Reset password</a>
You can use HTML in your mail like this :
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "<a href='http://example.com/PhpProject3/reset_passwd.php?key=".$email."&userid=".$userid."' style=' background-color: #4CAF50;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;'>Click To Reset password</a>";
$message .= "Other Information ";
// 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: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>
Please try this:
$html .= '<tr><td>Reject</td></tr>';
Try this
<a href='http://example.com/PhpProject3/reset_passwd.php?key=".$email."&userid=".$userid."' style=' background-color: #4CAF50;border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;'>
<button>Click To Reset password</button>
</a>";

send unique email content to multiple recipients using php mysql

I wanted to send unique confirmation link to multiple users, that user list get from the database mysql. I tried using the following code, but it send only the first user not all.
<?php
$get_ref_q = mysql_query("select * from $referal_details where ser_fk='$last_insert_id'");
$get_last_ref_value = mysql_num_rows($get_ref_q);
$z=1;
if($get_last_ref_value > 0)
{
//$user_email = array();
while($sel_per_fet = mysql_fetch_assoc($get_ref_q))
{
$rf_name = $sel_per_fet['rf_name'];
$dy_link = $sel_per_fet['dy_link'];
$user_email = $sel_per_fet['rf_email'];
$userhtml = '<html>
<body style="width:100%; font-family:Arial; font-size:13px; line-height:22px; background:#fff; position:relative;color:#555555; margin:0px; padding:0px;">
<div style="margin:0 auto; width:600px;">
<div style="background:#fff; width:594px; float:left;border:#2fb25d 3px solid;">
<div style="padding:27px; width:540px; float:left;text-align:center; border-bottom:#2fb25d 3px solid;">
</div>
<div style="background:#fff; padding:44px 34px; width:526px; float:left;">
<h1 style="color:#000; font-size:20px; font-family:Arial; font-weight:normal; margin-bottom:20px;">
Dear '.$rf_name.' <br />
</h1>
<p>Vetri has refered you for this servey</p>
<p>please find your servey link</p>
<p>'.$uri.'/index.php?token='.$dy_link.'</p>
</div>
</div>
</div>
</body>
</html>';
$user_subject = 'Vetri has refered you for this servey';
$user_message = $userhtml;
$user_headers .= 'MIME-Version: 1.0' . "\r\n";
$user_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$user_headers .= "From: Servey <$comp_email> \r\n";
mail($user_email,$user_subject,$user_message,$user_headers,"-f$comp_email");
$z++;
}
}
?>
Note: I already have $last_insert_id value. I wanted to send unique email content to each users, not the common contend. Thanks in advance.
Your code looks good - apart from one tiny issue:
$user_headers .= 'MIME-Version: 1.0' . "\r\n";
---------------^
This line does not need the ., and it is likely that subsequent emails are adding more and more identical headers to $user_headers, causing subsequent messages to fail.

Variable Not Working In While Loop

I am sending a newsletter using the following code I am going to post. I have a $to variable that is for email addresses in my database. I use a while loop to send an email for each email address in my database to preserve privacy. At the bottom of the email I have a link for unsubscribing which is linked to a simple script that has the users email in the link. The $to variable is not working in the link though. The email sends but when I look to see if it sent all the data the link looks like http://example.com/scripts/php/unsubscribe.php?email= instead of http://example.com/scripts/php/unsubscribe.php?email=example#email.com.
I'm not sure what I've done wrong here since I am getting no errors, and the script is working except for sending the email in the link.
require('/home/jollyrogerpcs/public_html/settings/globalVariables.php');
require('/home/jollyrogerpcs/public_html/settings/mysqli_connect.php');
mysqli_select_db($conn,"newsletterlist");
$query = "SELECT * FROM newsletterusers";
$result = mysqli_query($conn, $query);
$subject = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['subject']);
$message = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['body']);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Jesse Elser<jesse#jollyrogerpcs.com>' . "\r\n";
if (!$result) exit("The query did not succeded");
else {
while ($row = mysqli_fetch_array($result)) {
$to = $row['email'];
$date = date("m/d/Y h:i:sa");
$body ='<!DOCTYPE HTML>';
$body .='<body style="padding: 0; margin: 0; background-color: #000; color: #fff; text-align: center; font-family: verdana;">';
$body .='<div id="container" style="width: 90%; margin: 0 auto; text-align: left; background-color: #121212;">';
$body .='<div id="header" style="border-bottom: 1px solid #ff6400;">';
$body .='<img src="http://jollyrogerpcs.com/images/main/logo.png" width="100%">';
$body .='</div>';
$body .='<div id="subject" style="background-color: #121212; text-align: center;">';
$body .='<h1 style="color: #ff6400; margin: 0;">'.$subject.'</h1>';
$body .='</div>';
$body .='<div id="message" style="background-color: #232323; color: #fff; padding: 10px;">';
$body .= $message;
$body .='</div>';
$body .='<div id="footer" style="background-color: #121212; padding: 10px;">';
$body .='Visit Our Site | Thanks for subscribing to our newsletter! | Unsubscribe <br> E-mail sent: ';
$body .= $date;
$body .='</div>';
$body .='</body>';
mail($to,$subject,$body,$headers);
}
}
mysqli_close($conn);
header('Location: http://jollyrogerpcs.com/newsletter.php');
You are closing the href attribute before the email address is included so...
<a href="http://example.com/scripts/php/unsubscribe.php?email="'.$to.'"
Should be
<a href="http://example.com/scripts/php/unsubscribe.php?email='.$to.'"
As is it would render as
<a href="http://example.com/scripts/php/unsubscribe.php?email=" email#address.com"....
Which would make the link http://example.com/scripts/php/unsubscribe.php?email=.

Sending email in HTML [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I am setting up a confirmation email script for our customers to receive an email when they complete the online form. I uploaded my script and tested but I received an email with just the raw HTML. I have been looking at some tutorials trying to do this as well. I validated my HTML and I passed all the tests. Is there something that I have to include in my PHP for my body string to be interpreted as HTML?
Here is my script
<?php
//Script for sending a confirmation email
$body = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<title>Demystifying Email Design</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
</head>
<body style=\"margin: 0; padding: 0; background-color: #ecf0f1;\">
<table cellpadding=\"0\" cellspacing=\"0\" width=\"60%\" align=\"center\" style=\"border: 1px solid #bdc3c7;\">
<tr bgcolor=\"#3498db\">
<td align=\"center\">
<img alt=\"Logo\" src=\"http://www.****.com/logo-lg.jpg\" style=\"width: 50%; height: auto; padding-top: 5%; padding-bottom: 5%;\" />
</td>
</tr>
<tr bgcolor=\"#ffffff\" align=\"center\">
<td>
<h3 style=\"padding-top: 5%; padding-bottom: 5%;\">Worldwide innovator in flexible liquid packaging</h3>
</td>
</tr>
<tr align=\"center\" bgcolor=\"#ffffff\">
<td>
<h4 style=\"padding-top: 5%; padding-bottom: 5%;\">Thank you for contacting customer service. We've received your sample request; one of our team members will be in contact with you in the very near future. Thanks again for your time and interest.</h4>
</td>
</tr>
</table>
</body>
</html>";
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
if (mail($to,$subject,$body)){
echo "Mail was sent";
} else{
echo "Failed";
}
This is the email I received
You need to also set a header indicating this is HTML, e.g.
// 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: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Source (see Example 3).
Correct code will be:
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if (mail($to,$subject,$body,$headers)){
echo "Mail was sent";
}

Categories