Link to home
Start Free TrialLog in
Avatar of Starquest321
Starquest321

asked on

EMail Headers

I am having a huge issue with a cron job I wrote which sends EMail to SPAM. The cron actually runs - but for some reason it spits the emails into Users SPAM.

Here are the headers:

$strHeader .= 'From: '.$row['mediaemail'] . "\r\n";
$strHeader .= 'Reply-To: ' .$row['mediaemail']. "\r\n" ;
if($row['cc']!='') $strHeader .= 'Cc: '. $row['cc'] . "\r\n";
$strHeader .= 'X-Mailer: PHP/' . phpversion();
$strHeader .= "MIME-Version: 1.0\n";  
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
$strHeader .= "This is a multi-part message in MIME format.\n";  
 
$strHeader .= "--".$strSid."\n";  
$strHeader .= "Content-type: text/html; charset=utf-8\n";  
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
$strHeader .= $mailBodyText."\n\n";
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
Avatar of Starquest321
Starquest321

ASKER

Thanks - please check your email.
Couple of things here:
Received: from outbound-ss-183.bluehost.com (outbound-ss-183.bluehost.com. [69.89.29.197])

        by mx.google.com with SMTP id d3si13306108paw.273.2013.02.03.04.03.51;

        Sun, 03 Feb 2013 04:03:52 -0800 (PST)

Received-SPF: neutral (google.com: 69.89.29.197 is neither permitted nor denied by domain of ron@nadlan-investments.com) client-ip=69.89.29.197;

Authentication-Results: mx.google.com;

       spf=neutral (google.com: 69.89.29.197 is neither permitted nor denied by domain of ron@nadlan-investments.com) smtp.mail=ron@nadlan-investments.com

Message-Id: <510e5228.a34b420a.547b.41ceSMTPIN_ADDED_MISSING@mx.google.com>

Open in new window

As you can see, the SPF is neutral.  It would be better if the SPF said "pass!"  Here are the corresponding lines from a MailChimp broadcast email.
Received-SPF: pass (google.com: domain of bounce-mc.us4_7752489.333993-ray.paseur=gmail.com@mail77.us4.mcsv.net designates 205.201.128.77 as permitted sender) client-ip=205.201.128.77;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of bounce-mc.us4_7752489.333993-ray.paseur=gmail.com@mail77.us4.mcsv.net designates 205.201.128.77 as permitted sender) smtp.mail=bounce-mc.us4_7752489.333993-ray.paseur=gmail.com@mail77.us4.mcsv.net;
       dkim=pass header.i=brandon=3Dbrandonsavage.net@mail77.us4.mcsv.net
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; s=k1; d=mail77.us4.mcsv.net;

Open in new window

If you look at the first snippet, note the SMTPIN_ADDED_MISSING.  You probably want to do something about that.

These lines of code will create a single output line, all run together.  You might want a line-feed between the outputs.  As written it may cause the Mime-Version to be overlooked.

$strHeader .= 'X-Mailer: PHP/' . phpversion();
$strHeader .= "MIME-Version: 1.0\n";  

I am also a bit suspicious of this (though I am not sure about it):

Content-type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

I am fairly sure that utf-8 characters require more than 7 bits - some are multi-byte representations.  You might want to look into that part and see if these two lines collide in some way.

And finally if you want to send the email directly to me, straight out of the automated process, not via a forwarded message, there might be something else I could pick up.

HTH, ~Ray
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
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
I am attaching the full file for your review. Much much appreciated comments! Do you see any other errors in the code?
cron-lead-automessage.php
What DaveBaldwin said.  This line #156 needs to be revisited, and the construction of the message just above it should put the message body into a separate variable, so the message body can be put into the place where there is "null" right now.

if(mail( $row['leadEmail'] , $row['Subject'] , null, $strHeader )) {
In addition, the standard says that all lines in the header should be terminated by '\r\n' although many mail servers accept just '\n'.  In addition the 'boundaries', at least after the first, should be in the message body also.  Simple example here: http://www.enewsletterpro.com/articles/multi_part_mime_messages.asp

You might find that one of the reasons you're going in the spam folder is just because your messages aren't formatted properly.  That has been a frequent sign of spam.
We have updated the code - but still the issue is the same. Since the email is being sent from the host server I thought to check the php.ini file. This is the result:

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /usr/sbin/sendmail -t -i -f

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

Do I need to force extra parameters?
I am attaching the updated changed to the script - anything I missed? Still the spam issue.
cron-lead-automessage-updated.php
Here is another version - still not sending email!
cron-lead-automessage--2-.php
See if this runs for you on your web server.  This is my PHP Email demo that has run on Linux and Windows hosting.  Save it as 'Email.php' so it will post to itself and send email.  Put your own email address in for $toText.
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');

# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
<!-- 
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
-->
</style>
<script type="text/javascript">
<!--
function check()
{
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    {
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}
// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
		}
?>

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

<form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>

</body>
</html>

Open in new window

I just tested another version of my code thru the command line and it failed the first time.  I looked at my 'php.ini' and it looked like yours so I looked at phpinfo() thru the web server and it showed my machine's IP as the SMTP server.  That reminded me that I have IIS set up to work on that IP address and Not on localhost.  I changed 'php.ini' to look like this and now it works on the command line.  You only need to do this if you have setup IIS to work only on a specific IP address and not 'All Unassigned'.

[mail function]
; For Win32 only.
SMTP = 10.10.10.11
smtp_port = 25
David - I am using a host with linux . . . :( What should I do now?
If you have a SMTP server like 'sendmail' or 'Postfix' installed, you can just leave those settings alone.  I would leave 'sendmail_path' to the default value without the '-f' because you can add the '-f' in your code if it is needed.  Look at Example #3 on http://www.php.net/manual/en/function.mail.php except that sending no headers (null) will probably cause your email to bounce on most SMTP servers.

Did you try my demo on your server?
'PEAR' email is an entirely different method unrelated to using the PHP mail() function.
Is it better? Should I use that? Will that eliminate my issue?
ASKER CERTIFIED SOLUTION
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
Fair enough - I will test tomorrow and report back!
Looks like the issue is fixed! Thanks all for helping!
I just realized that I need to delete this question. My scripts post here include my actual user name/pass and domain information. I am a little stressed. How can I remove this?
Use the "Request Attention" button and a moderator will take care of it.  Probably want to change those anyway!
You need to contact the admins thru a Community support question.  I don't see a "Request Attention" button anymore.

Or you could just change the user name and password...
Going to the link above does not provide me with the ability to change the file.