Solved

PHP mail

Posted on 2016-07-21
6
53 Views
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.
0
Comment
Question by:rgranlund
6 Comments
 
LVL 49

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
0
 
LVL 6

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.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 41724106
Either:
1. The headers are so badly messed up that your mailer daemon (probably sendmail) can't figure out what to do.
OR
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:
https://www.experts-exchange.com/articles/10089/Trouble-Sending-Mail-from-Your-Script.html
https://www.experts-exchange.com/articles/1807/A-Crash-Course-in-Email-SMTP.html
https://www.experts-exchange.com/articles/1222/16-Tips-to-Improve-Email-Delivery.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 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.

<?php
error_reporting(E_ALL);
ini_set('display_errors','1');

# 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";
    $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 ($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">
  </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

0
 
LVL 108

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.
http://php.net/manual/en/book.mail.php

2. RTFM.  Make sure you understand the RFCs for internet messaging
http://www.faqs.org/rfcs/rfc2822.html
http://www.faqs.org/rfcs/rfc2047.html

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.
1
 
LVL 83

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.
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now