Solved

Don't send email

Posted on 2016-09-24
6
50 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:
Squinky 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 21

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:Squinky
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now