PHP mail

For some reason my PHP mail() wont send emails from the script when I include headers.  It is setup in my PHP.ini file but my emails wont go out.  Only if they are plain text.
Who is Participating?
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
Below is my PHP email demo which I think I have showed you before.  This works everywhere that I know of except for one place that did not have a SMTP server.  Save it as 'Email.php' so it can POST to itself.


# some settings of POST vars
if (!isset($_POST['submit']))  $submit = ''; else $submit = $_POST['submit'];
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 ($submit == "") {
    $title="Test Email Page";
else {
	if($fromText === "") die("No name!");
	$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"

<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;}
<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";
    //return false;
if (nam.length < 3)
    alerttxt=alerttxt+"Please enter your name.\r\n";
    //return false;
if (submitOK=="false")
    return false;

// -->

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

if ($submit != "") {
   	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="submit" name="submit" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>



Open in new window

Ryan ChongCommented:
need to know what's the error message you receiving?

and there's few possibilities:

1. your scripts contains error
2. your target sender email is invalid
3. the network connection issue which not able to send out emails
Mukesh YadavFull Stack DeveloperCommented:
Make sure if you are using from header then it's domain set to same as your domain you are using to send email.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

1. The headers are so badly messed up that your mailer daemon (probably sendmail) can't figure out what to do.
2. Your message is getting blocked by the recipient.

Email is extremely forgiving in terms of headers, so it's probably not #1. My guess is that the email message itself is getting sent but it's being blocked because the recipient server thinks it's spam. Check out some of my articles on email delivery:
Ray PaseurCommented:
Email is one of those things that seems so simple, and it was until about 2002.  Then we got the advent of non-plain-text emails, and with it came spam, viruses, malware, fraud, etc.  There are almost zero standards for email (it was conceived as an academic convenience, not a world-wide communications tool) and so the world, in a zero-standards, under-attack sort of way reacted willy-nilly to the onslaught of attacks.  More than a dozen years later, we still have almost zero standards, and a patchwork of hacks to "protect" us from bad email.  One good and useful tool is SPF.  Make sure you're using it correctly before you try anything else.  There are others (DKIM comes to mind) but I think SPF is a good place to start.

One of these protective hacks is the injection of anti-spam headers.  Your hosting company may be "helping" you by doing this.  You can get a dedicated server, and use your own mail system to get around this.  One reason you might want to get around this: The presence of any anti-spam header indicates that the message came from an automated script.  Many mail recipients automatically regard such messages as spam and discard them without notice.

Another of these hacks is the internal routing of email messages.  When mail is sent to certain domains that reside on the same server, the server provider may never actually send the messages out to the internet for routing.  If the recipient changes its mail DNS, the originating server never finds out, and the mail is lost.

The "additional headers" may be something that triggers a spam filter on the originating server.  Plain text email is almost never spam when compared to HTML email.  So the originating server provider may be inspecting the mail headers and discarding your messages if they contain certain signals.  Of course, the same thing can be expected on the receiving end.

For better or worse, you need hands-on access to the originating server in order to see what is really going on.  This is a multi-step process, but it will get you an answer.  You may not like the answer, and it may be necessary to change hosting providers, but first try this process.

1. RTFM.  Make sure you're not running up against any of the known issues with PHP mail.

2. RTFM.  Make sure you understand the RFCs for internet messaging

3. Deconstruct the messages.  Set up the SSCCE in a few different test scenarios.  One will be 100% plain text, the next will be plain text, with only a "From" header, the next will add a "Reply-to" header, etc.  You probably want to have several granular tests so you can detect exactly which header causes the failure.

4. Call your hosting provider and set up an appointment to watch the server as you run the scripts that send the mail.  As each script is run, record the result.  Once you know which header is causing the issue, come back here and post the script that uses that header.  Then we can work together to look for a way around the issue.
Dave BaldwinFixer of ProblemsCommented:
You may also find that your email provider may bounce or drop your email if it doesn't enough of the correct headers.  No headers used to be a big sign of home-brew spam.
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.