PHPmailer - Multiple sending of e-mail - php

I am sending emails using PHPmailer. As of now, I am successful in sending email to one address. Now, I want to send multiple emails in just one click.
PROBLEM: I have tried to use some loops below to send multiple email but I get wrong outpout. Yes, it sends email but to only one address, and the email address is getting all of the emails that are supposed to be emailed to other emails.
For example, when I send 17 emails, those 17 emails are sent to only one address. The emails should be sent according to the addresses in the database, with corresponding unique attachments. Example: abc#gmail.com should have abc.pdf attached, and 123#gmail.com should have 123.pdf attached.
I think it's in the loop. Please help me figure it out. Thanks.
require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php");
$mail = new PHPMailer();
$body = file_get_contents('phpmailer/body.html');
$body = preg_replace('/\/b]/','',$body);
$file ='phpmailer/mailpass.txt';
if($handle = fopen($file,"r")){
$contentpass = fread($handle,'15');
fclose($handle);
}
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "email#gmail.com";
$mail->Password = $contentpass;
$mail->SetFrom("email#gmail.com", "Subject");
$mail->AddReplyTo("email#gmail.com","Subject");
$mail->Subject = "Subjects";
$mail->AltBody = "Subject";
$mail->MsgHTML($body);
$file='current_schoolyear.txt';
if($handle = fopen($file,"r"))
{
$content = fread($handle,'9');
fclose($handle);
}
$input = addslashes($_POST['depchair']);
$email = "select email_address from sa_student where schoolyear = '$input'";
if ($p_address=mysql_query($email))
{
while($row = mysql_fetch_assoc($p_address))
{
$mail->AddAddress($row['email_address']);
$input = addslashes($_POST['depchair']);
$control = "select control_no from sa_student where schoolyear = '$input'";
if($ctrl=mysql_query($control)){
$ctrl_no = mysql_result($ctrl, 0);
$mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");
}
else
{
echo "No attached document.";
}
if(!$mail->Send()) {
$message = "<div class=\"nNote nFailure\" >
<p>Error sending email. " . $mail->ErrorInfo ."</p>
</div>";
} else {
$message = "<div class=\"nNote nSuccess\" >
<p> Email have been sent to the examinees in ".$input_depchair. "! </p>
</div>";
}
}
}
else
{
echo (mysql_error ());
}
UPDATED CODE: After running the code below, I was able to send an email and with the correct attachment. However, there was only ONE email sent (the last email address in the database), and the rest of the emails were not sent.
$input = addslashes($_POST['depchair']);
$email = "select email_address, control_no from sa_student where schoolyear = '$input'";
if ($p_address=mysql_query($email))
{
while($row = mysql_fetch_assoc($p_address))
{
$cloned = clone $mail;
$cloned->AddAddress($row['email_address']);
$cloned->AddAttachment("fpdf/pdf_reports/document/".$row['control_no'].".pdf");
if(!$cloned->Send()) {
$message = "<div class=\"nNote nFailure\" >
<p>Error sending email. " . $mail->ErrorInfo ."</p>
</div>";
} else {
$message = "<div class=\"nNote nSuccess\" >
<p> Email have been sent to the examinees in ".$input_depchair. "! </p>
</div>";
}
unset( $cloned );
}
}
else
{
echo (mysql_error ());
}

After you send an email $mail->Send(), execute this:
$mail->ClearAllRecipients();
in your while loop.
So your basic while loop structure looks like this:
while($row = mysql_fetch_assoc($p_address)){
$mail->AddAddress($row['email_address']);
$mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments(); //Remove all attachements
}

Within your loop, create a clone of the $mail object - before you add the recipient and attachment - then use the clone to send the email. The next loop iteration will create a new clone free of the previous address and attachment:
while($row = mysql_fetch_assoc($p_address)) {
$cloned = clone $mail;
$cloned->AddAddress($row['email_address']);
// add attchment to $cloned, etc.
if ( $cloned->send() ) { /* etc */ }
unset( $cloned );
}
This will "clear" your per-iteration changes (like address, attachment, etc) without you having to reenter config properties (like, from, host, etc.)
Addendum:
Your attachments will likely be all the same because you're not fetching new results for these lines (within your loop):
$input=addslashes($_POST['depchair']);
$control = "select control_no from sa_student where schoolyear = '$input'";
if ($ctrl=mysql_query($control)) {
$ctrl_no = mysql_result($ctrl, 0);
$mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");
}
$ctrl_no will always return the same result because (I'm assuming) $_POST['depchair'] does not change - thus $input, $control, $ctrl, and $ctrl_no all remain (effectively) the same for each loop. You need to find whatever it is your actually intend to be the $ctrl_no for each loop - right now you're using the same one over and over.
The following query could probably help:
// replace
// $email = "select email_address from sa_student where schoolyear = '$input'";
// with:
$students_query = "select email_address, control_no from sa_student where schoolyear = '$input'";
// then
// if ($p_address=mysql_query($email)) {
// while($row = mysql_fetch_assoc($p_address)) {
// becomes
if ( $students=mysql_query($students_query) ) {
while ( $row = mysql_fetch_assoc( $students ) ) {
// so that finally, etc
$cloned->AddAddress($row['email_address']);
$ctrl_no = $row['control_no'];
This pulls both the student email address and their control_no in the same query, making sure they stay associated with each other through the loop. You can then get rid of the second mid-loop query, since all the results you need were pulled in the first out-of-loop query. The above isn't all the code you need to change, just the critical parts.

Related

PHP strange behaviour on IF statement when sending email via phpmailer (having output of IF and ELSE together)

The script im going to present is quite dificult to understand, but I will explain the basic idea bellow.
<?php
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require_once 'apps/PHPMailer/src/Exception.php';
require_once 'apps/PHPMailer/src/PHPMailer.php';
require_once 'apps/PHPMailer/src/SMTP.php';
include_once('apps/HTMLDOMParser/simple_html_dom.php');
require_once("../db.php");
function clear($var) {
$var = NULL;
unset($var);
}
// Time when emails were sent last time
$result_laststamp = $conn->prepare("SELECT sent FROM nl_campaigns_emails ORDER BY sent DESC LIMIT 1", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$result_laststamp->execute();
if($result_laststamp->rowCount() > 0) {
while($row_laststamp = $result_laststamp->fetch(PDO::FETCH_ASSOC)) {
// data for first qeued campaign
$result_campaign = $conn->prepare("SELECT id,name,text,amount,period,smtp,user FROM nl_campaigns WHERE status='1' ORDER BY id ASC LIMIT 1", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$result_campaign->execute();
if($result_campaign->rowCount() > 0) {
while($row_campaign = $result_campaign->fetch(PDO::FETCH_ASSOC)) {
// SMTP Data
if($row_campaign['smtp']==1 && $row_campaign['user']>0) {
$result_smtp = $conn->prepare("SELECT * FROM admin_logins WHERE id=:id LIMIT 1", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$result_smtp->execute(array(":id"=>$row_campaign['user']));
if($result_smtp->rowCount() > 0) {
while($row_smtp = $result_smtp->fetch(PDO::FETCH_ASSOC)) {
// Check if its time to send next group of emails
if($row_laststamp['sent'] < (time()-($row_campaign['period']*3600))) {
// get email addresses, send, and edit timestmap in db when they were sent
$result_emails = $conn->prepare("SELECT id,email FROM nl_campaigns_emails WHERE (id_campaign=:id_campaign AND sent='0') ORDER BY id ASC LIMIT ".$row_campaign['amount'], array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$result_emails->execute(array(":id_campaign"=>$row_campaign['id']));
if($result_emails->rowCount() > 0) {
$array_email_ids = array();
while($row_emails = $result_emails->fetch(PDO::FETCH_ASSOC)) {
try {
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8'; // UTF8 Encoding
$mail->Encoding = 'base64'; // Needs to be set with UTF8
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = "smtp.somedomain.com"; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "noreply#somedomain.cz"; // SMTP username
$mail->Password = "somepassword"; // SMTP password
$mail->SMTPSecure = "tls";
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Sender
$mail->setFrom($row_smtp['email'], 'somename');
$mail->addReplyTo('info#somedomain.cz', 'somename');
$mail->Sender = 'noreply#somedomain.cz';
// Recipient
$mail->addAddress($row_emails['email']);
// Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$bottom_text = "<p align=center style=\"font-size:11px; font-family:Arial; color:#8b8b8b;\">This email was sent to address ".$row_emails['email'].".<br>";
$bottom_text .= "To unsubscribe you can click to unsubscribe.</p>";
$bottom_text .= "<img src=\"http://www.somedomain.cz/view.php?id_campaign=".$row_campaign['id']."&id_email=".$row_emails['id']."\" style=\"visibility: hidden;\">";
// YOUTUBE LINK
$text = new simple_html_dom();
$text->load($row_campaign['text']);
foreach($text->find('iframe') as $element) {
$youtubeid = substr($element->src, strrpos($element->src, "/")+1);
// Create image instances
$video = imagecreatefromjpeg('https://img.youtube.com/vi/'.$youtubeid.'/maxresdefault.jpg');
$button = imagecreatefrompng('../images/youtube_play.png');
$video = imagescale($video, $element->width, $element->height);
$button = imagescale($button, imagesx($video)/7.5);
imagecopy($video, $button, imagesx($video)/2-imagesx($button)/2, imagesy($video)/2-imagesy($button)/2, 0, 0, imagesx($button), imagesy($button));
// Output and free from memory
imagejpeg($video, "../images/campaigns/videos/".$youtubeid.".jpg", 95);
imagedestroy($video);
imagedestroy($button);
unset($video);
unset($button);
$element->outertext = '<img src="http://www.somedomain.cz/images/campaigns/videos/'.$youtubeid.'.jpg" width="'.$element->width.'" height="'.$element->height.'">';
}
$row_campaign['text'] = (string)$text;
$text->clear();
clear($text);
// URL LINK FOR CLICKER ANALYZER
$doc = new simple_html_dom();
$doc->load($row_campaign['text']);
foreach ($doc->find('a') as $a) {
$a->href = 'http://www.somedomain.cz/clicker.php?id_campaign='.$row_campaign["id"].'&id_email='.$row_emails["id"].'&url='.urlencode($a->href);
}
$row_campaign['text'] = (string)$doc;
$doc->clear();
clear($doc);
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $row_campaign['name'];
$mail->Body = $row_campaign['text'].$bottom_text;
$mail->AltBody = strip_tags($row_campaign['text']);
// MessageID
$data = array(
'campaign_id' => str_pad($row_campaign['id'], 6, '0', STR_PAD_LEFT),
'email_id' => str_pad($row_emails['id'], 11, '0', STR_PAD_LEFT),
'sent' => time(),
);
$messageId = '<'.base64_encode(json_encode($data)).'#www.somedomain.cz>';
$mail->MessageID = $messageId;
$mail->addCustomHeader('In-Reply-To', $messageId);
clear($data);
// SEND Email
$mail->send();
}
catch (Exception $e) {
echo "Emails were not send because: {$mail->ErrorInfo}";
die();
}
// ADD sent email to array
array_push($array_email_ids, $row_emails['id']);
// UPDATE the "sent" stamp
$update = $conn->prepare("UPDATE nl_campaigns_emails SET sent=:sent WHERE id=:id");
try { $update->execute(array(":id"=>$row_emails['id'], ":sent"=>time())); }
catch(Exception $error) { die($error->getMessage()); }
clear($update);
clear($mail);
}
clear($result_emails);
// CHECK IF STATUS COULD BE CLOSED
$result_emails_sent = $conn->prepare("SELECT id FROM nl_campaigns_emails WHERE (id_campaign=:id_campaign AND sent='0') ORDER BY id", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$result_emails_sent->execute(array(":id_campaign"=>$row_campaign['id']));
if($result_emails_sent->rowCount() == 0) {
$update = $conn->prepare("UPDATE nl_campaigns SET status=:status WHERE id=:id");
try { $update->execute(array(":status"=>3, ":id"=>$row_campaign['id'])); }
catch(Exception $error) { die($error->getMessage()); }
clear($update);
}
clear($result_emails_sent);
// Print the result
echo "<div style=\"color:#1b8d3d; text-align:center; font-size:25px; font-family:Arial;\">Sent total: ".count($array_email_ids)." emails.</div>";
echo "<center>REFRESH</center>";
}
}
else {
echo "<div style=\"color:#1b8d3d; text-align:center; font-size:25px; font-family:Arial;\">Next emails can be send at: ".date("j.n.Y H:i:s", $row_laststamp['sent']+($row_campaign['period']*3600))."</div>";
echo "<center>REFRESH</center>";
die();
}
}
}
else { die("error resolving SMTP"); }
clear($result_smtp);
}
}
clear($result_campaign);
}
else {
echo "<div style=\"color:#1b8d3d; text-align:center; font-size:25px; font-family:Arial;\">No campaign is running</div>";
echo "<center>REFRESH</center>";
die();
}
}
}
clear($result_laststamp);
clear($conn);
?>
the script looks quite huge, but at the end its simple logic:
get the last time emails were sent (for example $lastTimeSent)
get the data of the opened queued campaign (for example $dataCampaign)
get SMTP data to be used for sending emails (for example $SMTP)
checks if current timestamp time() is bigger then $lastTimeSent+allowed_period (because Im allowed to send 200 emails every 20mins)
loop the phpmailer, send emails separately (each time add one email to send)
and the problem is, that if I run the script inside nonallowed time period (so im not following rule of 200 emails in 20 mins), all works fine and script will evaluate this as false:
if($row_laststamp['sent'] < (time()-($row_campaign['period']*3600)))
$row_laststamp['sent'] is the timestmap of last email sent ($lastTimeSent)
$row_campaign['period'] is allowed period to send the email (value is actually 0.33 as in hours its 20 minutes), and im multiplying it by 3600 to recalculate to seconds so I can compare
so basicaly will jump and do stuff only from ELSE statement as its logical and its OK behaviour
However, when im outsode of the period so the same statement would evaluate as TRUE, the very strange behaviour happens.
The script will be looping and sending emails as it should, however instead of sending 200 emails as its the value of $row_campaign['amount'], it actually sends 64 (sometimes 65 emails) and then it wont echo Sent total: ".count($array_email_ids)." emails as it should logicaly, but also shows again the ELSE output *Next emails can be send at: ".date("j.n.Y H:i:s", $row_laststamp['sent']+($row_campaign['period']3600))..
So Im dealing with the problem that its shows the IF and ELSE output at once...
In other words, when statement *if($row_laststamp['sent'] < (time()-($row_campaign['period']3600))) is TRUE, it will partialy run the loop of try {} (only 65 times out of 200) and then shows the ELSE {} anyway...
BUT !!! if I remove/comment the line $mail->send(); then ALL works as expected !!!!! (unless emails are sent of course).
I was checking the memory status if im not getting out of memory and found out the maximum in final loop is about 24MB (when limnit is set to 256MB), also execution time is around 10second when I have set 120s.
so basicaly PHPMailer wont send more then 200 emails in loop when sending just one each time?
I wouldnt mind to send them all together and not looping it, however i dont want them to be seen in "TO" header of email, and if I use BCC instead, then (at least in gmail) it jumps to spam.
Any ideas?
EDIT:
I have simplified the script, made the adjustment according to https://github.com/PHPMailer/PHPMailer/wiki/Sending-to-lists
also i did place the debugger $mail->SMTPDebug = SMTP::DEBUG_SERVER; to see whats happening, and its interesting that the message from client to server suddently stop in the middle... no responce from the server, so the transfer just stops in the middle...
the very last line is not even finished and it just:
2020-09-02 17:06:40 CLIENT -> SERVER: NTI1MjUyNTNGaWRfY2FtcGFpZ24lMjUyNTI1MjUyNTI1MjUyNTI1MjUyNTI1MjUyNTI1MjU
so the comunication just stops from no reason, no server reply that its sent or not, nothing...
anyone has an idea?
so the error at the end was realy that I was not clearing recipients after each send(),
therefore first email was send to 1 recipient and each loop was 1 added but previous was not cleared...
clearing recipients after each loop fixed the problem

Files corrupted

I have a google apps scripts app that gets Gmail message attachments, posts it to DB using JDBC. then on the server, a PHP script gets the data and puts in into a file and attaches it to an email.
the problem is that the files are corrupt when email arrives
here is the google apps script function that gets the attachment content
function getMessageAttachmentsArray(msg){
var GmailAttachments = msg.GmailMessage.getAttachments();
var validAttachments = [];
var attachmentNames = [];
if(GmailAttachments)
{
for(i in GmailAttachments)
{
var gName = GmailAttachments[i].getName();
attachmentNames.push(gName);
var mimeType = GmailAttachments[i].getContentType();
var size = GmailAttachments[i].getSize();
var content = Utilities.base64Encode(GmailAttachments[i].getDataAsString(), Utilities.Charset.UTF_8);
var push = {"content":content,"mimeType":mimeType,"fileName":gName,"size":size,"id":""};
validAttachments.push(push);
}
}
return [validAttachments, attachmentNames];
}
here is the PHP code that generated the email from the file data:
require_once 'smtpmail/classes/class.phpmailer.php';
$mail = new PHPmailer(true);
$email = $argv[1];
$messageid = $argv[2];
$fax_number = $argv[3];
$attachments = array();
//get the attachments for this email
$Sql = "select * from user_attachments where email = '$email' and messageid like '$messageid%'";
$res = mysql_query($Sql);
while($row = mysql_fetch_array($res)){
$return['filename'] = $row['name'];
$return['mime'] = $row['mime_type'];
$content = base64_decode(str_pad(strtr($row['raw_data'], '-_', '+/'), strlen($row['raw_data']) % 4, '=', STR_PAD_RIGHT));
$temp_file = tempnam(sys_get_temp_dir(), 'Fax');
file_put_contents($temp_file, $content);
$return['file'] = $temp_file;
array_push($attachments, $return);
}
try{
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SetFrom("example#example.com", "example email");
$mail->Subject = '';
$mail->Body = ' '; //put in a blank body to avoid smtp error
$mail->AddAddress($email);
foreach($attachments as $file){
$mail->AddAttachment($file['file'], $file['filename'], 'base64' ,mime_content_type($file['file']));
}
if($mail->send()){
echo "email to $email sent successfully\n";
}else{
echo "error sending email to $email\n";
}
}catch(phpmailerException $e){
echo $e->errorMessage();
}catch(Exception $e){
echo $e->getMessage();
}
When the message is received it shows the attachments but when downloaded I can not open them and there is a message that the file is corrupt or the file extension does not match the file format
what am I doing wrong?
Thanks in advance.
EDIT:
I tried emailing the attachment without posting to the DB, by posting to the server with UrlFetchApp() and the results are the same. clearly, I am doing something wrong with Base64_encode / decode...
maybe the google apps scripts :
Utilities.base64Encode(GmailAttachments[i].getDataAsString(), Utilities.Charset.UTF_8);
creates a different base64 format than PHP base64_decode expects?
p.s.
I tried also with and without 'str_pad' and I still got the same results.
I changed:
Utilities.base64Encode(GmailAttachments[i].getDataAsString(), Utilities.Charset.UTF_8);
to:
Utilities.base64Encode(GmailAttachments[i].getBytes());
and it works

PHPMailer adding multiple emails to BCC from MYSQL

In below code, we are first selecting users based on the admin's input from database. Then sending emails to those users. With the code it sends emails to $mail_news->addAddress('testuser#gmail.com'); test user. But for the bcc part is not working, as it doesn't send any emails to bcc email users.
foreach($email_array as $news_mail){
$mail_news->AddBCC($news_mail.";");
}
This is how we fetch user emails via form & PHP prepare statement with mysqli.
Here is the main part code:
if($msn->execute()){
$msn->store_result();
$msn->bind_result($news_mail);
while($msn->fetch()){
$email_array[] = $news_mail;
}
// echo "successful";
}
else
{
echo "database failed";
}
//--Email Sending Starts
$mail_news = new PHPMailer;
$mail_news->isSMTP();
$mail_news->Host = EMAIL_HOST;
$mail_news->SMTPAuth = true;
$mail_news->Port = EMAIL_PORT;
$mail_news->SMTPSecure = 'tls';
$mail_news->Username = EMAIL_ADD;
$mail_news->Password = EMAIL_PASS;
$mail_news->From = EMAIL_ADD;
$mail_news->FromName = 'Company Account';
$mail_news->addAddress('testuser#gmail.com');
foreach($email_array as $news_mail){
$mail_news->AddBCC($news_mail.";");
}
$mail_news->WordWrap = 50;
// $mail_news->SMTPDebug = 2;
$mail_news->isHTML(true);
$mail_news->Subject = "".$sub;
$mail_news->Body = "".$body;
$mail_news->AltBody = "".$altbody;
if(!$mail_news->send()) {
echo "Failed Sending Emails" ;
echo 'Mailer Error: ' . $mail_news->ErrorInfo;
} else {
echo "All Email sending completed" ;
}
?>
</form>
<?php
$msn->close(); // Finally closing the database
}
?>
You need to change the line
$mail_news->AddBCC($news_mail.";");
with
$mail_news->AddBCC($news_mail);
because the method addBCC() handles the semicolon by itself. You do not need to specify by your own.

dynamic content in Email using PHPmailer

I have this mailing list from php mailer examples: https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps
so I put the foreach loop within a while loop so it iterates through the entire table, I'm using a token array to replace the name and any dynamic content, the content is working, but it keeps putting the wrong name in the email.
The name and email are inserted into vac_mailing_system table correctly. but when it's selected it seems to grab a random name and assign it to a random email address. So this makes me think there is an issue with the select statement.
$finish_table = $dbh->prepare("SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts < 5");
if($finish_table->execute()) {
$finish_table->setFetchMode(PDO::FETCH_ASSOC);
}
while($row = $finish_table->fetch()) {
$vac_id = $row['vac_id'];
$vac_name = $row['vac_name'];
$vac_comp = $row['vac_comp'];
$full_name = $row['full_name'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = '';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = ;
$mail->Username = '';
$mail->Password = '';
$mail->setFrom('', 'Information');
$mail->addReplyTo('', ' Information');
$mail->Subject = "Information";
$mail->Body = strtr(file_get_contents('new_vac_email.html'), array('%full_name%' => $full_name, '%vac_id%' => $vac_id, '%vac_name%' => $vac_name, '%vac_comp%' => $vac_comp));
//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you're using a templating system),
//set it inside the loop
//msgHTML also sets AltBody, but if you want a custom one, set it afterwards
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
//Connect to the database and select the recipients from your mailing list that have not yet been sent to
//You'll need to alter this to match your database
$mysql = mysqli_connect('host', 'user', 'pass');
mysqli_select_db($mysql, 'db_name');
$result = mysqli_query($mysql, "SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts < 5 LIMIT 0, 50");
foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+
$mail->addAddress($row['email_address'], $full_name);
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("#", "#", $row["email_address"]) . ') ' . $mail->ErrorInfo . '<br />';
//Mark it as sent in the DB
mysqli_query(
$mysql,
"UPDATE vac_mailing_system SET sent_status = false, retry_attempts = +1 WHERE email_address = '" .
mysqli_real_escape_string($mysql, $row['email_address']) . "'"
);
break; //Abandon sending
} else {
echo "Message sent to :" . $full_name . ' (' . str_replace("#", "#", $row['email_address']) . ')<br />';
//Mark it as sent in the DB
mysqli_query(
$mysql,
"UPDATE vac_mailing_system SET sent_status = true WHERE email_address = '" .
mysqli_real_escape_string($mysql, $full_name) . "'"
);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
$mail->clearAttachments();
}
sleep(60);
echo "emails sent";
}
Try this code:
Yes, very easily with include and a short helper function:
function get_include_contents($filename, $variablesToMakeLocal) {
extract($variablesToMakeLocal);
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('new_vac_email.php', $data); // HTML -> PHP!
$mail->Send(); // send message
Try this code:
// first we will create small helper function
function get_include_contents($filename, $variablesToMakeLocal) {
extract($variablesToMakeLocal);
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have an event today";
// rename your new_vac_email.html file to your new_vac_email.php
// create a data array which contains replace values
// here user_name is variable name in new_vac_email.php
//Adarsh hatkar is value which is shown in place of <?php echo ($user_name); ?>
$data = array('user_name' => 'Adarsh hatkar' );
$mail->Body = get_include_contents('new_vac_email.php', $data);
$mail->Send(); // send message
demo new_vac_email.php file code for example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Email Confirmation</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body >
<p>Hello <?php echo ($user_name); ?> </P>
</body>
</html>

Creating a Craigslist Anonymous Email Forwarding Program

The following program is intended to match incoming email aliases with those in the database, and forward the email to the right address, like Craigslist does.
I am now getting this error:
Error: [1] You must provide at least one recipient email address.
in anon-email.php at line number: sending the email
Here is the code:
$mailboxinfo = imap_mailboxmsginfo($connection);
$messageCount = $mailboxinfo->Nmsgs; //Number of emails in the inbox
for ($MID = 1; $MID <= $messageCount; $MID++)
{
$EmailHeaders = imap_headerinfo($connection, $MID); //Save all of the header information
$Body = imap_qprint(imap_fetchbody($connection, $MID, 1)); //The body of the email to be forwarded
$MessageSentToAllArray = $EmailHeaders->to; //Grab the “TO” header
$MessageSentToAllObject = $MessageSentToAllArray[0];
$MessageSentToMailbox = $MessageSentToAllObject->mailbox ."#". $MessageSentToAllObject->host; //Everything before and after the “#” of the recipient
$MessageSentFromAllArray = $EmailHeaders->from; //Grab the “FROM” header
$MessageSentFromAllObject = $MessageSentFromAllArray[0];
$MessageSentFromMailbox = $MessageSentFromAllObject->mailbox ."#". $MessageSentFromAllObject->host; //Everything before and after the “#” of the sender
$MessageSentFromName = $MessageSentFromAllObject->personal; //The name of the person who sent the email
$toArray = searchRecipient($MessageSentToMailbox); //Find the correct person to send the email to
if($toArray == FALSE) //If the alias they entered doesn’t exist…
{
$bounceback = 'Sorry the email in your message does not appear to be correct';
/* Send a bounceback email */
$mail = new PHPMailer(); // defaults to using php “mail()”
$mail -> ContentType = 'text/plain'; //Plain email
$mail -> IsHTML(false); //No HTML
$the_body = wordWrap($bounceback, 70); //Word wrap to 70 characters for formatting
$from_email_address = 'name#domain.com';
$mail->AddReplyTo($from_email_address, "domain.Com");
$mail->SetFrom($from_email_address, "domain.Com");
$address = $MessageSentFromMailbox; //Who we’re sending the email to
$mail->AddAddress($address, $MessageSentFromName);
$mail->Subject = 'Request'; //Subject of the email
$mail->Body = $the_body;
if(!$mail->Send()) //If the mail fails, send to customError
{
customError(1, $mail->ErrorInfo, "anon-email.php", "sending the email");
}
}
else //If the candidate address exists, forward on the email
{
$mail = new PHPMailer(); // defaults to using php “mail()”
$mail -> ContentType = 'text/plain'; //Plain E-mail
$mail -> IsHTML(FALSE); //No HTML
$the_body = wordwrap($Body, 70); //Wordwrap for proper email formatting
$from_email_address = "$MessageSentFromMailbox";
$mail->AddReplyTo($from_email_address);
$mail->SetFrom($from_email_address);
$address = $toArray[1]; //Who we’re sending the email to
$mail->AddAddress($address, $toArray[0]); //The name of the person we’re sending to
$mail->Subject = $EmailHeaders->subject; //Subject of the email
$mail->Body = ($the_body);
if(!$mail->Send()) //If mail fails, go to the custom error
{
customError(1, $mail->ErrorInfo, "anon-email.php", "sending the email");
}
}
/* Mark the email for deletion after processing */
imap_delete($connection, $MID);
}
imap_expunge($connection); // Expunge processes all of the emails marked to be deleted
imap_close($connection);
function searchRecipient() // function to search the database for the real email
{
global $MessageSentToMailbox; // bring in the alias email
$email_addr = mysql_query("SELECT email FROM tbl WHERE source='$MessageSentToMailbox'"); // temp store of the real email
$row = mysql_fetch_array($email_addr); //making temp store of data for use in program
if(empty($row['email']))
{
return FALSE;
}
else /* Else, return find the person's name and return both in an array */
{
$results = mysql_query("SELECT * FROM tbl WHERE email = '$email_addr'"); // temp store of both queries from this function
$row = mysql_fetch_array($results, $email_addr); //making temp store of data for use in program
$name = $row['author']; // taking the author data and naming its variable
return array($name, $email_addr); // this is the name and the real email address to be used in function call
}
}
function customError($errno, $errstr, $file, $line)
{
error_log("Error: [$errno] $errstr in $file at line number: $line",1, "name#domain.com","From: name#domain.com.com");
die();
}
Here is the first thing I would try:
It would appear that your function searchRecipient isn't being passed a parameter. Rather than use the global keyword, I would define it in your function call. Also, mysql_fetch_array does not pass back an associative array, which is what you are using in your next step. I would change that to mysql_fetch_assoc (it's the same thing essentially). There are also a few other minor syntax corrections in this function. Here are my proposed changes to that function. I think this should fix your problem. Or at least get you moving forward.
function searchRecipient($MessageSentToMailbox) // function to search the database for the real email
{
$email_addr = mysql_query("SELECT email FROM tbl WHERE source='$MessageSentToMailbox'"); // temp store of the real email
$row = mysql_fetch_assoc($email_addr); //making temp store of data for use in program
if(empty($row['email']))
{
return FALSE;
}
else /* Else, return find the person's name and return both in an array */
{
$email_addr = $row['email'];
$results = mysql_query("SELECT * FROM tbl WHERE email = '$email_addr'"); // temp store of both queries from this function
$row = mysql_fetch_assoc($results); //making temp store of data for use in program
$name = $row['author']; // taking the author data and naming its variable
return array($name, $email_addr); // this is the name and the real email address to be used in function call
}
}
You could also combine this into one query and make it a little easier. Here is that solution.
function searchRecipient($MessageSentToMailbox)
{
$results = mysql_query("SELECT email, author FROM tbl WHERE source='$MessageSentToMailbox'");
$row = mysql_fetch_assoc($results);
if(empty($row['email']) || empty($row['author'])) return false;
return array($row['email'], $row['author']);
}

Categories