Mail Not Sent

Using php mail. Php program in code (in debug mode).
<?
require_once('class.phpmailer.php');
function date_conv($x) {
	$dt = substr($x,5,2) . "/" . substr($x,8,2) . "/" . substr($x,0,4);
	return $dt;
}
session_start();
if ($_SESSION['gvhaadm'] == "") {
	header ("location: admin_login.php");
	exit;
}
$tdiff = time() - $_SESSION['alast_used'];
if ($tdiff > 2400) {
	session_destroy();	
	header ("location: admin_login.php");
	exit;
}
$_SESSION['alast_used'] = time();	
$mail             = new PHPMailer(); // defaults to using php "mail()"	
// set up database
$Host = "####REMOVED####";
$User = "####REMOVED####";
$Password = "####REMOVED####";
$DBName = "####REMOVED####";
$Link = mysql_connect ($Host, $User, $Password);
$qry = "SELECT * from homeowner where email <> '' and lname1 <> '' order by lname1";
$res = mysql_db_query ($DBName, $qry, $Link);
$nh = mysql_num_rows($res);
$attach = "";
$fname = "attach";
if ($_FILES[$fname]['name'] != "") {
	$attach = basename($_FILES[$fname]['name']);
	$uploadfile = "attachments/" . $attach;
	move_uploaded_file($_FILES[$fname]['tmp_name'], $uploadfile); 
} 
if($_POST['repl'] == "on") {
	$frhd = "From: " . $_SESSION['admemail'] . "\r\n";
} else {
	$frhd = "From: ####REMOVED####@gvha.biz\r\n";
}	
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= $frhd;	
$headers .= "Cc: richard@rkassociates.com\r\n";
$subj = $_POST['subject'];
$both = false;
$board = false;
$arch = false;
if ($attach != "") {
	$path = "attachments/";
	$file = $attach;
	$pf = $path . $file;
	$from = "####REMOVED####@gvha.biz";
	$frname = "";
}
if ($_POST['test'] == "on") {
	$to = "####REMOVED####@gmail.com, ####REMOVED####@cox.net";
	$mail->AddAddress("####REMOVED####@gmail.com", "Richard Korts");
	$mail->AddAddress("####REMOVED####@cox.net", "Jim Boland");
	//echo "attach = " . $attach . "<br>";
	$msg = $_POST['email'];
	if ($attach != "") {
		$mail->AddAttachment($pf);
		if(!$mail->Send()) {
			echo "Mailer Error: " . $mail->ErrorInfo;
		}
	} else {
		echo "to = " . $to , ",subj = " . $subj . "', msg = " . $msg . "headers = " . $headers . "<br>";
		$mres = mail($to, $subj, $msg, $headers);
		echo "mres return = " . $mres . "<br>";
	}
	$nh = 2;
	header("Location: admin_menu.php?nes=" . $nh);
	exit;
}		
if ($_POST['all'] != "on") {
	if ($_POST['board'] == "on") {
		if ($_POST['arch'] =="on") {
			$both = true;
		} else {
			$board = true;
		}
	} else {
		if ($_POST['arch'] =="on") {
			$arch = true;
		}
	}	
	if ($both) {
		$qry = "SELECT * from homeowner where email <> '' and (board <> '' or arch_comm <> '')";
	}
	if ($board) {
		$qry = "SELECT * from homeowner where email <> '' and board <> ''";
	}
	if ($arch) {
		$qry = "SELECT * from homeowner where email <> '' and arch_comm <> ''";
	}
	$res = mysql_db_query ($DBName, $qry, $Link);
	$nh = mysql_num_rows($res);
}
//echo "email body: " . $_POST['email'] . "<br>";
if ($nh < 10) {
	$ng = 1;
} else {
	$ng = 1 + intval($nh / 10);
}
//echo "ng = " . $ng . "<br>";
for ($i = 0; $i < $ng; $i++) {
	//echo "group = " . $i . "<br>";
	$mail = new PHPMailer();
	$mail->SetFrom('####REMOVED####', 'GVHA Board');
	$mail->Subject = $subj;
	$mail->MsgHTML($_POST['email']);
//echo "email body: " . $msg . "<br>";
	if ($attach != "") {
		$mail->AddAttachment($pf);
	}
	if ($i == $ng - 1) {
		$js = 10 * ($ng - 1);
		$je = $nh;
	} else {
		$js = 10 * $i;
		$je = $js + 10;
	}
	//echo "js = " . $js . "<br>";
	//echo "je = " . $je . "<br>";
	for ($j = $js; $j < $je; $j++) {		
		$h = mysql_fetch_array($res);
		//echo "j = " . $j . ", email = " . $h['email'] . "<br>";
		if ($attach != "") {
			$mail->AddAddress($h['email'], $h['fname1'] . " " . $h['lname1']);
		} else {
			$tostr = $h['email'];
			$subj = $_POST['subject'];
			$mres = mail($tostr, $subj, $_POST['email'], $headers);
		}
	}
	if ($attach != "") {	
		$mail->AddCC("####REMOVED####");
		if(!$mail->Send()) {
			echo "Mailer Error: " . $mail->ErrorInfo;
		//} else {
			//echo "Group " . $i . " Message sent!";
		}
	}
}	

header("Location: admin_menu.php?nes=" . $nh);
exit;	
?>

Open in new window


When I run it I get this (see echos): (I understand the last warning)

Mail returns 1, NOTHING is received.

What's wrong?

to = ####REMOVED####@gmail.com, ####REMOVED####@cox.net,subj = Test Message # 3', msg = Test # 3headers = MIME-Version: 1.0 Content-type: text/html; charset=iso-8859-1 From: ####REMOVED####@gvha.biz Cc: ####REMOVED####@rkassociates.com
mres return = 1

Warning: Cannot modify header information - headers already sent by (output started at /home/content/21/10338421/html/email_sendn.php:68) in /home/content/21/10338421/html/email_sendn.php on line 73
Richard KortsAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Good question, Julian!  $attach is unconditionally set to an empty string on line 29, and the only code that changes it is inside a conditional, making this a data-dependent problem.  From the look of the comment on line 60, it looks like this question has come up before.

Also I just noticed that this script has the short-open tag.  That should probably get fixed before it becomes a problem.

The reason that Constant Contact is a better idea than trying to write your own mailer scripts is exactly the same reason why it's better to go to a doctor than trying to do your own surgery.  Doctors have years of training and experience, and so does Constant Contact.  Doctors specialize in medicine; Constant Contact specializes in getting email to work.  I've been through this process, and believe me, I am never again going to build an app that is dependent on my own email systems.  There are just too many things that can go wrong.  Constant Contact has a full-time staff devoted to making sure none of those things go wrong.  You can't do what they do, but you can engage their services.  The results will be exactly what you want, easy to use, and almost embarrassingly inexpensive.

If you're insistent on trying to debug a failing email system, here are some of the questions you must answer.

1. Does the PHP mail() command work at all?  
2. What mail server is PHPMailer using?  How can you verify that it is working?
3. What if the mail is being sent, but a DNS router is wrong?  How will you know?
4. What if the receiving mail program is using a whitelist?  How will you know?  How will you get on the whitelist?
5. What if the receiving mail program is using a blacklist?  Are you on it because of a prior malformed email message?  How will you know?
6. What if your hosting company injects anti-spam headers into your messages?  How can you find these headers?  What will each of the mail relays do with these headers?
7. What if the receiving mail server is down?  How will you verify receipt of the mail?
8. Is your mail even getting out of your hosting company to the internet?  How will you know?

For each of these issues, you must set up the SSCCE and test it.  Unfortunately, you cannot test alone - you need the cooperation of server administrators and security officers who work for other companies.  You may reasonably expect to have to pay them for their time helping you.

And once you get all of the moving parts right, and you get an email message to go through, then you have only to wait a while (sometimes a short while) before something else goes wrong.

I don't have any financial interest in Constant Contact.  They are a publicly traded company.  There are competitors.  The only reason I recommend them is because my experience as a customer has been excellent.

The point of all this, Richard, is not to discourage you.  Just hoping to point up a better and easier way than trying to do all this yourself!
0
 
Ray PaseurCommented:
Warning: Cannot modify header information...
https://www.experts-exchange.com/articles/4423/Warning-Cannot-modify-header-information-headers-already-sent.html
$Link = mysql_connect...
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
$res = mysql_db_query...
http://php.net/manual/en/function.mysql-db-query.php

PHP mail() and email in general has no accountability, and mail messages with even a hint of automation or error, as evinced in their headers or structure, are likely to be discarded.  About a decade ago you could expect the messages to be bounced, with some kind of automated rejection notice to the Reply-to address, but this is not done much any more because malformed email is almost universally recognized to be spam.  And once a mail source has been identified as a spammer, it's very unlikely that any more mail messages will be accepted from the same source, no matter how well-formed the new messages might be.

Suggest you discard this code and convert the entire application over to use Constant Contact.  it will take some time and money, but not very much of either.  And the end results will be much, much better than trying to do this yourself.
1
 
Richard KortsAuthor Commented:
Ray,

I forgot I had the old mysql stuff. I fixed that.

I understand the reason for Warning: Cannot modify header information...

I already echoed, so i KNOW that won't work.

No way I have the time to screw with Constant Contact.

Can someone else please chip in?

Thanks
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
As Ray has mentioned, the lack of accountability with PHP mail makes it nearly impossible to troubleshoot something as complicated as this. I've banged my head against the wall in similar situations. The only solution is to begin with a very simple email with static parameters and build up from there.
0
 
Julian HansenCommented:
I see a logic problem in that you have to points at which you issue a $mail->Send()
Line 64 and 139

Both are inside a conditional
if ($attach != "") {	

Open in new window


So is it possible that in your test $attach is empty?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.