send unique email content to multiple recipients using php mysql - php

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.

Related

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

Is it a good idea to store css classes in php strings when sending html emails through php

Some email providers like gmail do not support css classes when sending html email through php, so I thought i would store css classes or even full codes in php strings so I can use them multiple times. For example
$leftbox = '<div style="background:#333;border:1px solid #000;padding:5px 2px;float:left;width:200px;">';
$rightbox = '<div style="background:#FFF;border:1px solid #CCC;padding:5px 2px;float:right;width:200px;">';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = '<html><body style="background:#000;margin:auto;width:400px;">';
$message .= $leftbox.'Name </div>'.$rightbox.' Content 1</div>';
$message .= $leftbox.'Addresss </div>'.$rightbox.' Content 2</div>';
$message .= $leftbox.'Mobile </div>'.$rightbox.' Content 3</div>';
mail("emailadress#email.com", "Subject", $message, $headers);
Is this a good idea, or is there any other good ways to do this if you want a good looking html email sent with php?
What is wrong with an inline style sheet? For example:
<!DOCTYPE html><html>
<head>
<style type="text/css">
body {
background:#000;
margin:auto;
width:400px
}
.leftBox {
background:#333;
border:1px solid #000;
padding:5px 2px;
float:left;
width:200px
}
</style>
</head>
<body>
<div class="leftBox">Something in the left box</div>
</body>
</html>
Edit:
Just had a look at some HTML email on 'Inbox' and you may need to include a <!DOCTYPE html>tag. Other than that, the above code "works for me".

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

sending html email using php / outlook not displaying html properly

i am using php to send a html email like so in my send_email.php file:
<?php
// multiple recipients
$to = 'mark.obrien2014#inbox.com';
// subject
$subject = 'Hewden New Supplier Setup';
// message
$message = file_get_contents('../../assets/email/email.php');
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mark <mark.obrien2014#inbox.com>' . "\r\n";
$headers .= 'From: Hewden New Supplier Setup <NewSuppliers#hewden.co.uk>' . "\r\n";
$headers .= 'Cc: purchasing#hewden.co.uk' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I am including my email html in a seperate file email.php using
$message = file_get_contents('../../assets/email/email.php');
I am testing my email has been sent using Outlook 2010 on windows and also testing it in a normal webmail. The email sends fine on both but in outlook the html doesn't seem to display properly. My email looks fine in every other email program and all other webmail. can someone please show me what i am doing wrong? thanks in advance
my email html:
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body leftpadding="0" offset="0" paddingheight="0" paddingwidth="0" toppadding="0" style="padding-top: 0; padding-bottom: 0; padding-top: 0; padding-bottom: 0; background:#F6F6F6; width: 100% !important; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; -webkit-font-smoothing: antialiased;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table align="center" class='bgBody' border="0" cellpadding="0" cellspacing="0" style="font-family:helvetica, sans-serif; background:#fff; border:1px solid #999; margin-top:50px;" width="750">
<tr>
<td align="center">
<img src="http://hewdenportal.co.uk/assets/email/images/bigImg.png" width="750" height="258" />
</td>
</tr>
</table>
</tr>
</table>
<table align="center" class='bgBody' border="0" cellpadding="0" cellspacing="0" style="font-family:helvetica, sans-serif; font-weight:100; font-size:16px; background:#000; color:#4f4f4f; border:1px solid #999; margin-top:20px;" width="752">
<tr>
<td align="left">
<div class="container" style="min-height:200px; width:690; background:#fff; padding-left:30px; padding-right:30px; padding-top:20px; padding-bottom:20px; font-family:helvetica, sans-serif; font-weight:100; font-size:15px;">
<div class="text" style="width:72%;">
<h1 style="font-family:helvetica, sans-serif; font-weight:100; font-size:20px;">Getting Started</h1>
<p>Thanks for taking an Interest in joining Hewden as an approved supplier.<br/>We care about providing a good quality to service to Customer's and Supplier's alike.</p>
<p>As part of the Hewden journey blah blah blah.</p>
</div>
<div class="find_more" style="width:110px; border:1px solid #666; height:20px; background:rgba(255,195,82,1); margin-top:10px; float:right; position:relative; cursor:pointer; cursor:hand; text-align:center; padding:6px;">Find out More ></div>
</div>
</td>
</tr>
</table>
</body>
</html>
email in webmail showing correct html:
email in outlook with distorted html:
Use or <button> tags instead of a <div> tag.
As far as I know (I send HTML emails pretty often), buttons are well more accepted on Outlook and other email providers.
HTML emails are very annoying sometimes... :)
Using div in html emails is a little tricky and if you can get away with table that'd be easier in general. I would guess the specific issue here is that Outlook 2010 doesn't support width, position, or float for div. Campaign Monitor wrote a nice roundup of div compatibility here.

Categories