Solved

Don't send email

Posted on 2016-09-24
6
62 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Only run code if on a certain page 11 34
PHP mail() function not working... 8 33
Wordpress and Wufoo 1 39
What is define("__APPROOT__", __DIR__); 6 30
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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