Issues with mail() command

I wrote a PHP program to automate sending messages to an address list I maintain in a mySQL database. SOMETIMES the mail is tagged as SPAM; however, I can send a message directly from my gMail account to the recipients who have told me my automated mail is tagged as SPAM, so I am guessing something may be missing from the headers that identifies mail as legitimate.

Here’s my code, and all suggestions are welcome (I have removed personal info from the code below; actual Subject and Content contains specific information, and is conversational, not pitching anything):

SORRY FOR THE POOR FORMATTING – I’M A NEWBIE HERE, AND UNSURE OF THE BEST WAY TO ATTACH CODE

<?php


/* All form fields are automatically passed to the PHP script through the array $_POST. */
$SentFrom   = $_POST['SentFrom'];

/* Other variables to set up eMail addresses  */
$__AT__ = "@";
$__DOT__ = ".";
$eol = "\r\n";

$email6 = 'My.Name' . $__AT__ . 'gMail' . $__DOT__ . 'com';

/*  Headers for message */
$headers = 'From: My Name <' . $email6 . '>' . $eol;
$headers .= 'Reply-to: My Name <' . $email6 . '>' . $eol;
$headers .= 'Return-Path: My Name <' . $email6 . '>' . $eol;    // these two to set reply address 
$headers .= 'Message-ID: <' . $now . ' TheSystem@' . $_SERVER['SERVER_NAME'] . '>' . $eol; 
$headers .= 'X-Mailer: PHP v' . phpversion() . $eol;          // These two to help avoid spam-filters 

$headers .= 'Content-type: text/html; charset=us-ascii' . $eol;

/* Reply To Address */
$additional = "[email protected]";

$todays_date = date("Y-m-d");
$today = strtotime($todays_date);


/*  Content (I have removed personal info for the purpose of this post)  */
$Subject = 'eMail Testing';

$msg = 'THIS IS A TEST. (actual messages have relevant content from database)'  . $eol;;


/* ---------------------------------------

MAIL() COMMAND is...
mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )

------------------------------------------ */

	if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $SentFrom)) {
	  $ErrorTrap .= "...Invalid email address: " . $recipientAddress . $eol;
	} elseif ($RecipientName == "") {
	  $ErrorTrap .= "...No Name: " . $recipientAddress . $eol;
	} elseif ($GreetingName == "") {
	  $ErrorTrap .= "...No Greeting Name: " . $recipientAddress . $eol;
	} elseif ($ProductionChecked == "0") {
	  $ErrorTrap .= "...Production NOT CHECKED: " . $ProductionChecked . $eol;
	}

/* Sends the mail to each recipient and captures the ResultMsg string. */

	elseif (mail($recipientAddress,$Subject,$msg,$headers,$additional)) {
		$Counter = $Counter + 1;
		$uQuery="UPDATE Recipients SET DateSent=CURDATE(), Priority=$SentPriority WHERE EmailAddress='$recipientAddress'";
		mysqli_query($link, $uQuery);
		sleep(8);
	} else {
		$ErrorTrap .= "<tr><td nowrap>NOT Sent: </td><td nowrap>" . $RecipientName . "</td><td nowrap>" . $recipientAddress . "</td></tr>" . $eol;
		$Failed = $Failed + 1 ;
	}
}


//--- Pause n seconds 
sleep(2);

//--- Resume


mysqli_close($link);

?>

Edit By Benanamen: Added Code Tags

Simple…

In the world of SPAM, the server’s account is most important. If you send emails out from a server, they must have the FROM set to the server’s account, not your personal, non-server, account.

Since you have a server, you should be able to create at least one email account on it. Then, use that as the FROM address. You can use your own email account as the Return-to path. The return address is not usually an issue.

For further info, I did have one account on a shared server system that I had to have the return path also set to the server’s account. Then, I just set the server’s email account to forward all mail to my personal account. This worked fine as the world knew the email was valid from the server, but, I personally got all the mail. Also, this hid my personal account from the world.

Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service