Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of Richard Korts

ASKER

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
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.
Avatar of Julian Hansen
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?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial