Go Premium for a chance to win a PS4. Enter to Win


PHP mail

Posted on 2016-07-21
Medium Priority
Last Modified: 2016-08-09
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.
Question by:rgranlund
LVL 54

Expert Comment

by:Ryan Chong
ID: 41724076
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

Expert Comment

by:Mukesh Yadav
ID: 41724087
Make sure if you are using from header then it's domain set to same as your domain you are using to send email.
LVL 35

Expert Comment

ID: 41724106
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:
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 84

Accepted Solution

Dave Baldwin earned 2000 total points
ID: 41724181
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

LVL 111

Expert Comment

by:Ray Paseur
ID: 41724488
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.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41724545
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

782 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