Page 1 of 2

mail() working sporadically

Posted: Mon Feb 28, 2011 3:20 pm
by sleepydad
I have a simple script to send HTML email. For some reason, it sends well one time and delays (if it sends at all) for hours another time. I've Googled the topic, and see that I'm not alone but have not found a concrete solution. Don't know if it makes a difference, but I'm using Linux hosting on Go Daddy.

Here's the script:

Code: Select all

$headers  = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1"."\r\n";
$headers .= "From: sleepydad;
$headers .= "<me@me.com>\r\n";
$headers .= "Reply-To: me@me.com"\r\n";
$headers .= "Return-Path: me@me.com;

$to='someone@someone.com';
$subject='test';
$body='hello world';

mail($to, $subject, $body, $headers);
FYI - I've tried removing the first two lines of $header and attempted to send without HTML formatting with same (mixed) results.

Thanks in advance for your assistance -
sleepydad

Re: mail() working sporadically

Posted: Mon Feb 28, 2011 4:00 pm
by Loougle
I'm going through the exact same problem. I have about 7 php contact forms like this and I get some emails but most I do not.

This morning I sent a test mail through a php contact form and it worked. A client just now used the same exact one and I am not getting them. I tried the other php contact forms and none of them did.

Re: mail() working sporadically

Posted: Mon Feb 28, 2011 4:26 pm
by AbraCadaver
There are some other headers that might be needed so the emails don't look like spam. Also there is an option for sendmail that tells what to add for the envelope sender. The apache user may need some privilege to do this though, not sure:

Code: Select all

$headers .=  'X-Mailer: PHP/' . phpversion();
$options  = '-f me@me.com';
mail($to, $subject, $body, $headers, $options);

Re: mail() working sporadically

Posted: Mon Feb 28, 2011 4:49 pm
by sleepydad
Thanks very much for the suggestion, Abra. Unfortunately, it didn't make a difference ... dang. I'm all ears if anyone has any other ideas.

Thanks again!

Re: mail() working sporadically

Posted: Mon Feb 28, 2011 7:07 pm
by sleepydad
FYI copy/pasted from Go Daddy's help page ...

"Our mail server will not send email containing a "From:" header entry of aol, gmail, yahoo, hotmail, live, aim, or msn."

I guess I need to find an alternative to php's mail() now.

Suggestions, anyone?

Re: mail() working sporadically

Posted: Mon Feb 28, 2011 7:46 pm
by Weirdan
sleepydad wrote:Suggestions, anyone?
Swiftmailer: https://github.com/swiftmailer/swiftmailer

Re: mail() working sporadically

Posted: Mon Feb 28, 2011 9:27 pm
by sleepydad
Thanks Weirdan. I happened across Swift Mailer only a half an hour before I received your reply. Do you know much about configuring it? I was reading this:

http://code.google.com/p/swiftmailer/

but am not sure how to configure this portion of the script that reads:

$smtp = Swift_SmtpTransport::newInstance('smtp.host.tld', 25)
->setUsername(' ... ')
->setPassword(' ... ');

Where do I get this information? Should I be able to send a basic HTML email using the code in the link that I listed above? I only ask that because the documentation on Swift Mailer's site show's a much lengthier script.

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 3:32 am
by Weirdan
sleepydad wrote: but am not sure how to configure this portion of the script that reads:

$smtp = Swift_SmtpTransport::newInstance('smtp.host.tld', 25)
->setUsername(' ... ')
->setPassword(' ... ');

Where do I get this information?
From someone who controls the mail server(s) for a domain which you want your messages to originate from. For example, to send messages from @gmail.com you need to find out their smtp server address on google's help pages (google 'swiftmailer gmail' - there should be ready-made examples). To send mail from your own domain - contact your hosting provider and ask for smtp server address and if you need to provide any login/password to it.
Should I be able to send a basic HTML email using the code in the link that I listed above?
Yes, you should.

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 10:23 am
by sleepydad
okay, I'm using

Code: Select all

<?php


require_once 'lib/swift_required.php';

$smtp = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
  ->setUsername('myuser@gmail.com')
  ->setPassword('password'); // omitted here for security

$mailer = Swift_Mailer::newInstance($smtp);

$message = Swift_Message::newInstance('Hello World');
$message
  ->setTo(array(
    ///'user1@example.org',
    'me@sbcglobal.net' => 'sleepydad',
    //'user3@exmaple.org' => 'Another User Name'
  ))
  ->setFrom(array('myfriend@yahoo.com' => 'Sam'))
  //->attach(Swift_Attachment::fromPath('/path/to/doc1.pdf'))
  //->attach(Swift_Attachment::fromPath('/path/to/doc2.pdf'))
  ->setBody(
    //'Here is an image <img src="' . $message->embed(Swift_Image::fromPath('/path/to/image.jpg')) . '" />',
    'Here is an image',
    'text/html'
  )
  //->addPart('This is the alternative part', 'text/plain')
  ;

if ($mailer->send($message))
{
  echo "Message sent!";
}
else
{
  echo "Message could not be sent.";
}



?>
And I get the following:

Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 (Connection timed out) in /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/StreamBuffer.php on line 233

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' in /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/StreamBuffer.php:235 Stack trace: #0 /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/StreamBuffer.php(70): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/AbstractSmtpTransport.php(101): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start() #3 /home/content/s/l/e/sleepydad/html/kasebergkronies/swiftmail.php(30): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/StreamBuffer.php on line 235

I tried socket 25 as was written in the example that I downloaded, but that didn't work. I Googled the issue and someone suggested socket 465. That's not working either. I think I'm close here. Can I bother you one more time?

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 10:56 am
by Weirdan
sleepydad wrote:I tried socket 25 as was written in the example that I downloaded, but that didn't work. I Googled the issue and someone suggested socket 465. That's not working either. I think I'm close here. Can I bother you one more time?
I believe you need to specify the connection is encrypted, like this:

Code: Select all

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'sslv2') // <======== third parameter
        ->setUsername($account['email'])
        ->setPassword($account['password']);

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 11:53 am
by sleepydad
Sorry, still no go. This is beyond frustrating! I've posted the question in a couple other forums and will post a solution here for future reference if I get one. I REALLY do appreciate your time and efforts!

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 12:02 pm
by John Cartwright
sleepydad wrote:Sorry, still no go. This is beyond frustrating! I've posted the question in a couple other forums and will post a solution here for future reference if I get one. I REALLY do appreciate your time and efforts!
When something doesn't work, especially after someone proposed a solution, please post the result and/or error messages. :wink:

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 12:29 pm
by sleepydad
Sorry, getting same error message as noted earlier:

"Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 (Connection timed out) in /home/content/s/l/e/sleepydad/html/kasebergkronies/lib/classes/Swift/Transport/StreamBuffer.php on line 233 ..."

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 1:27 pm
by Loougle
ON my contact forms I'm not even using headers, should I be using headers and could that be the reason why I only receive the first few submissions and then not receiving the rest?

Re: mail() working sporadically

Posted: Tue Mar 01, 2011 1:41 pm
by John Cartwright
Loougle wrote:ON my contact forms I'm not even using headers, should I be using headers and could that be the reason why I only receive the first few submissions and then not receiving the rest?
Please don't threadjack.