Link to home
Start Free TrialLog in
Avatar of Torquil Beavis
Torquil BeavisFlag for Canada

asked on

php mail fails after working

Hi:

I had mail() working in my registration script. The testers started, and the first tester got the activation email message correctly on their gmail account that they registered with. It contains the activation code and URL to enter their email address, pw and this code.

When the second tester registered using their gmail account, they did not receive the email. So I tested the script by registering myself using one of my gmail accounts and it failed, which is odd since it worked previously.

So I scripted a simple mail() as follows:
<?php
// Emailing test

$to = "my.name@gmail.com";
$subject = "Subject: This is the subject";
$message = "This is the message";
$headers = "From: support@example.com"."\r\n"."Reply-to: support@example.com"."\r\n";

mail($to, $subject, $message, $headers);

echo "mailed";
?>

Open in new window


and it failed.
Here are the php.ini mail settings which I have never changed from the default on my shared server:
;php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; 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 =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =

Open in new window


I made sure that I whitelisted my site and the support email address on gmail. When I send identical data from my personal email to the one used by my site, it is accepted.

Any ideas?
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

What are you defining as "fail" here?  Are you actually seeing an error in the error_log to indicate a failure or is the mail just not showing up?

If the latter, it's probably more likely that proceduely generated email sent directly from the server is being spam-trapped by something before it ever gets to Google and to your personal settings.  

I've basically stopped using the PHP mail() command for this reason and instead I sign up all sites I work on for Mandrill (mandrill.com) and use them as the sending server.  With Mandrill and properly configured SPF and DKIM records you will see almost perfect delivery rates.
Avatar of Torquil Beavis

ASKER

No errors that I can see in the log, and it fails to arrive at the gmail address.
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
About headers:  It is typical that email from web sites don't use complete headers but email from 'regular' email clients always do.  Simple headers has been a sign of spam for many years.
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
Thanks guys.
I copied your script, Dave, and it replied 'sent', yet my gmail address did not receive it. Could this be a google gmail issue or perhaps a site blocking issue of some sort (I am verified by google's webmaster tools)?
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
Interesting.  My email demo can't send from this computer to Gmail... but it works fine on my web hosting at Hurricane Electric.  Where is your web site and server located?  Are you on 'public hosting' or are you on your own computer on a residential account?

Note that the Cc: to one of my other email addresses went thru even when it did not go to Gmail.
I'm on shared hosting out of BC, Canada. I don't use any VPS. It's odd that they'd blacklist since I only sent individual emails from the website, and only around 30 in total over the last 2 months. The last one to go through went to an email account that wasn't mine, then the failed one also went to an email that wasn't mine. I'd been testing the script for a while using my own 8 gmail accounts, then added 2 more for 2 more testers.
I'll check with the web host re IP blacklisting.
I'm using the email as an activation process only. All member communications will be through the Contact page and will be put onto DB.
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
Might also want to learn about this.  If you don't already use SPF headers you may want to try it.
http://en.wikipedia.org/wiki/Sender_Policy_Framework
Note that if you are on Godaddy hosting, PHPMailer isn't going to help you because they block all ports but port 25 and that is only available to connect to their mail servers.  You are essentially blocked from connecting to an outside mail server on other ports.
Probably should be using 587 or one of the secure ports anyway...
I'm not on GoDaddy. I checked for IP blacklisting and my site is ok.

I'm testing phpmailer. I checked all the variables with the web hosting company. I tried using the IP address instead of smtp.example.com. It failed. Follows is the script and fail message ..
<?php

require_once('includes/class.phpmailer.php');
include('includes/class.smtp.php'); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

//$body             = file_get_contents('contents.html');
//$body             = eregi_replace("[\]",'',$body);
$body="Hello there!";

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.example.com"; // SMTP server      Alternatively, use smtp.example.com  OR IP address
$mail->SMTPDebug  = 3;                     // enables SMTP debug information (for testing)  -  >>>>>>>> change to 2 when testing done <<<<<<<<<<<
// 1 = errors and messages
// 2 = messages only
// 3 = verbose errors
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = 'none';
$mail->Host       = "smtp.example.com"; // sets the SMTP server    Alternatively, use smtp.example.com OR IP address
$mail->Port       = 587;                    // set the SMTP port
$mail->Username   = "example@example.com"; // SMTP account username
$mail->Password   = "abc123";        // SMTP account password
$mail->SetFrom("example@example.com", "Sally");
$mail->AddReplyTo("example@example.com", "Sally");
$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->Body = $body;
//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
//$mail->MsgHTML($body);
$address = "my.accountname@gmail.com";
$mail->AddAddress($address, "krotb");
//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send())
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    echo "Message sent!";
}
?>

Open in new window


And here's the error message ..
2015-04-07 21:29:21 Connection: opening to smtp.example.com:587, t=300, opt=array ( ) 2015-04-07 21:29:21 SMTP ERROR: Failed to connect to server: Connection refused (111) 2015-04-07 21:29:21 SMTP connect() failed. Mailer Error: SMTP connect() failed.      

Open in new window


All ideas welcome.
In place of 'smtp.example.com', you need to use a valid SMTP server address where your username and password are valid.  You also need to verify that port 587 is an acceptable port on that server.  All instances of 'example@example.com' need to be replaced with valid email addresses.
According to the web host the smtp address is valid (not 'example' of course), since it's the mail server. They confirmed that my username and pw are correct; and they emphasized 587 as the port for outgoing email. Also, the email addresses are all valid/real (not 'example'). I simply sterilized the code for this post ;)
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
I sent an email from my personal gmail account to the web site support@example.com which also forwards to another gmail account. 100% success. This makes it seem as if the script has an error, like a missing parameter or invalid value, that's preventing it connecting. Or am I missing something here?
So I tried an example of a phpmailer for gmail where I used 'to' and 'from' gmail addresses, and still got a fail:
<?php

require_once('includes/class.phpmailer.php');
include('includes/class.smtp.php'); // optional, gets called from within class.phpmailer.php if not already loaded

define('GUSER', 'example1@gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password FOR example@gmail.com

$mail = new PHPMailer();

$body="Hello there!";
$from = "example1@gmail.com";
$from_name = "James";
$subject = "This is a test gmail";
$body = "Using the gmail phpmailer";
$to = "example2@gmail.com";

$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail->Send())
{
    echo 'Mail error: '.$mail->ErrorInfo;
}
else
{
    echo 'Message sent!';
}
?>

Open in new window


Error:
Mail error: SMTP connect() failed.

Open in new window


This does not use any of the website's email addresses, yet still failed.
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
Is this what you mean, Jason?

<?php

require_once('includes/class.phpmailer.php');
include('includes/class.smtp.php'); // optional, gets called from within class.phpmailer.php if not already loaded

define('GUSER', 'my.gmail@gmail.com'); // GMail username for google account
define('GPWD', 'password'); // GMail password

$mail = new PHPMailer();

$body="Hello there!";
$from = "support@example.com"; // where example.com is my website
$from_name = "James";
$subject = "This is a test gmail";
$body = "Using the gmail phpmailer";
$to = "member@gmail.com";

$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail->Send())
{
    echo 'Mail error: '.$mail->ErrorInfo;
}
else
{
    echo 'Message sent!';
}
?>

Open in new window


This was the response:
Mail error: SMTP connect() failed.

Open in new window


I believe this script is using Gmail's SMTP server. Did I misunderstand what you meant?
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
This is what I ran .. still failed .. message below

<?php

require_once('includes/class.phpmailer.php');
include('includes/class.smtp.php'); // optional, gets called from within class.phpmailer.php if not already loaded

define('GUSER', 'my.gmail@gmail.com'); // GMail username for google account
define('GPWD', 'password'); // GMail password

$mail = new PHPMailer();

$body="Hello there!";
$from = "my.gmail@gmail.com";
$from_name = "James";
$subject = "This is a test gmail";
$body = "Using the gmail phpmailer";
$to = "member@gmail.com";

$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail->Send())
{
    echo 'Mail error: '.$mail->ErrorInfo;
}
else
{
    echo 'Message sent!';
}
?>

Open in new window

2015-04-08 19:11:36 SMTP ERROR: Failed to connect to server: Connection refused (111) 2015-04-08 19:11:36 SMTP connect() failed. Mail error: SMTP connect() failed.

Open in new window

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
Ok, I checked the mxtoolbox site and it's green from all 89 blacklists.

mail() was working in my script while testing. Then I got two non-IT testers to run the site. The first one got the email, while later that day, the second one did not get the email. It was around the time that gmail had major issues supposedly for a few hours.
Thought: Could there be php.ini parameter settings that are preventing the connection to the server?
Or permissions somewhere, perhaps?
Could a redirect from non-SSL to SSL in .htaccess cause this issue? OK, I'm over the edge of the envelope now (since it worked before). Browsed through the net and can't find anything that could lead to this. Does the (111) error give a hint?
No, it's not an .htaccess issue.  It may be something in PHP.ini or it could be an ISP issue.  We've kind of reached the limit on what it we can do without direct access to the server.

The scripts looked correct, this should have worked...
It's working now.

I had family in from abroad with their 4 devices (plus our 5). Two of them were testers. All went well until one of the testers didn't get the email in question.

In a state of complete frustration with this (and I know you empathize), and since family have now left, I turned off the router and computer, then powered up again. I tested the email() in the app and it worked perfectly across emails from the website (using the original mail()).  

I take from this that the router was somehow corrupting the emailing. Any ideas why?

Clearly, I'm going to take your advice and use phpmailer since I don't need unexpected email failures from any source.
I take from this that the router was somehow corrupting the emailing. Any ideas why?

Beats the heck out of me.  Maybe Dave or Ray have an idea, but this should not have happened...
Since I'm new to experts exchange, do I need to post anew for help with phpmailer (since it's throwing up an error I don't understand), in order to replace mail() with phpmailer in my app?
I would.  This site works best as one question per thread.  Once it gets more than a couple of days old, it's harder to get new eyeballs in here.  Posting a new question sends new alerts to all available Experts in the topic and gets more help, faster.
All of your helps showed me what the issue was not, which was good. Much appreciated guys.