Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Mail Not Sent

Posted on 2016-10-24
Medium Priority
Last Modified: 2016-11-01
Using php mail. Php program in code (in debug mode).
function date_conv($x) {
	$dt = substr($x,5,2) . "/" . substr($x,8,2) . "/" . substr($x,0,4);
	return $dt;
if ($_SESSION['gvhaadm'] == "") {
	header ("location: admin_login.php");
$tdiff = time() - $_SESSION['alast_used'];
if ($tdiff > 2400) {
	header ("location: admin_login.php");
$_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 != "") {
		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);
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;
//echo "email body: " . $msg . "<br>";
	if ($attach != "") {
	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 != "") {	
		if(!$mail->Send()) {
			echo "Mailer Error: " . $mail->ErrorInfo;
		//} else {
			//echo "Group " . $i . " Message sent!";

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

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
Question by:Richard Korts
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
LVL 111

Expert Comment

by:Ray Paseur
ID: 41857834
Warning: Cannot modify header information...
$Link = mysql_connect...
$res = mysql_db_query...

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.

Author Comment

by:Richard Korts
ID: 41857862

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?

LVL 22

Expert Comment

by:Kim Walker
ID: 41857979
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.
LVL 59

Expert Comment

by:Julian Hansen
ID: 41858095
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?
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 41858567
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!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Explore the encryption capabilities built into Google Apps and how these features can help you meet privacy policy and regulatory compliance, but are not a full solution. Understand and compare the most popular email encryption services for Google A…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Suggested Courses

636 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