Solved

Don't send email

Posted on 2016-09-24
6
58 Views
Last Modified: 2016-09-27
I am working on a project that will go live next week. Each "rep" will get an email with one or two or no attachments.

We have never tested this because a "live" test will actually send the emails.

The code that follows is what I have put together as a test version. Note the LACK of $mail->Send() (or at least that is my intention).

I'm not trusting my own perception because so many things have gone wrong on this project.

I need some other eyes to look at this code & tell me "no emails will be sent".
<?php
	// email_RFQs.php
date_default_timezone_set("America/Los_Angeles");
require( 'wp-load.php' );
global $wpdb;
$dir = getcwd();
$pdfdir = $dir . "/pdfs/";
$odir = $dir . "/opdfs/";
//echo "dir = " . $dir . "<br>";
require_once('class.phpmailer.php');
if (strpos($dir, "staging") !== false) {
	$loc = "http://watermanusa.staging.wpengine.com";
} else {
	$loc = "http://www.watermanusa.com";
}
$qry = "SELECT * from auth_users";
$users = $wpdb->get_results($qry, ARRAY_A);	
$i = 0;
foreach($users as $user) {
	$entity = $user['entity'];
	$ocode = $user['code'];
	if ($entity == "") {
		$ocode = "All";
		$entity = "All";
	}	
	$mail            = new PHPMailer(); // defaults to using php "mail()"

	$mail->isSMTP(); // telling the class to use SMTP transport
	$mail->SetFrom('noreply@watermanusa.com');
	$body             = "Dear " . $user['name'] . "<br><br>";
	$body = $body . "Greetings!<br><br>";
	$body = $body . "Here is your weekly update of quotes in process with Waterman Industries estimating department.   Please take a minute to review the status of your projects.  Check in with our estimating team if you have questions or concerns.<br><br>";
	$body = $body . "Thank you for your efforts toward growing our mutual success in your region.<br><br>";
	$body = $body . "The Team at Waterman Industries";
	$mail->Subject = "Your Weekly Status Report from Waterman Industries";
	$mail->AddAddress($user['email']);
	$someAttach = false;
	$file = $pdfdir . $entity . ".pdf";
	//echo "attach = " . $file . "<br>";
	//echo "email = " . $user['email'] . "<br>";
	if (file_exists ($file)) {
		$someAttach = true;
		$mail->AddAttachment($file);
	}
	$ofile = $odir . $ocode . "orders.pdf";
	if (file_exists ($ofile)) {
		$someAttach = true;
		$mail->AddAttachment($ofile);
	}
	if (! $someAttach) {
		$body = $body . "<br><br>There are currently no RFQ's or orders for " . $entity;
	}	
	$mail->MsgHTML($body);
	if ($someAttach) {
		echo $user['email'] . " will get RFQ file: " . $file . " and order file: " . $ofile . "<br>";
	} else {
		echo $user['email'] . " will get no attachments.<br>";
	}	
}	
?>

Open in new window


Thanks
0
Comment
Question by:Richard Korts
6 Comments
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 500 total points
ID: 41814025
Yes, if you don't call send(), nothing will be sent. I'd also suggest you look at the mailing list example provided with PHPMailer; It's very similar to what you're doing, but is far more efficient. Incidentally it also looks like you've based your code on an obsolete example, so make sure you're running the latest version of PHPMailer from github.
1
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814130
For testing the actual sending of the email, you could adjust the query:
$qry = "SELECT * from auth_users";

Open in new window

to something like this which just targets your own email address:
$qry = "SELECT * from auth_users where email='myemail@example.com'";

Open in new window

and output the user details from the for loop using echo like you've got in the code (but is commented out).

Once you've got that confirmed as just getting your own email address, you can add the call to send() and test the actual sending of the email.

When wanting to go-live, obviously just change the query back to what it was.
0
 

Author Comment

by:Richard Korts
ID: 41814196
Terry,

Yes, we have done something like that (more intricate) before; I am more concerned with which attachments will go to which reps; thus the echos.

Richard
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 22

Expert Comment

by:Kim Walker
ID: 41814294
The only true test is to actually send an email -- even if the email is hijacked to a test email account. You can easily do this by modifying your select query to select a specific email address rather than the one in the data.
SELECT `entity`, `code`, `name`, "test@email.com" AS `email`, FROM `auth_users`

Open in new window

One thing that struck me as a potential hazard when I perused your code was that your are looping through every record in the auth_users table. But that is apparently your objective. If either of two PDFs exists, they will be attached to the email. Otherwise, they will get an email notification that no PDFs exist. That made me wonder if this routine would be followed by a routine to purge or archive the existing PDFs.
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 41814926
Something I use all the time for testing PHPMailer is to use a fake mail server like FakeSMTP. You can run this on some high numbered port then point your script at it and let it send - all your messages will simply show up in the mail server's local UI so you can inspect exactly what's being sent without any risk of messages being sent to real recipients. This is covered in the PHPMailer docs & examples.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 41815041
In cases like this, I will look to see if I put my app in testing mode or live.   My my config.php file I may set a variable such as

$appStatus = "live"  ' turn features on or set in testing mode

Open in new window


Then when needed to change for live or testing

if($appStatus = "live"){
     
         	$mail->AddAddress($user['email']);

}  else {

                $mail->AddAddress('USE_A_TESTING_GMAIL_ACCOUNT@GMAIL.COM');

}

Open in new window


I also will set up a 2nd site just for testing either on a subdomain or another domain where I always keep in testing mode and of course the live site in live mode.  That way if something breaks down the road, you can test it out on your testing site before pushing it live.

If you are going to have a lot of email, (more than a hundred) then limit your query to the top 100
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question