mail() working sporadically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

mail() working sporadically

Post 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
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: mail() working sporadically

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mail() working sporadically

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: mail() working sporadically

Post 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!
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: mail() working sporadically

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: mail() working sporadically

Post by Weirdan »

sleepydad wrote:Suggestions, anyone?
Swiftmailer: https://github.com/swiftmailer/swiftmailer
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: mail() working sporadically

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: mail() working sporadically

Post 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.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: mail() working sporadically

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: mail() working sporadically

Post 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']);
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: mail() working sporadically

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: mail() working sporadically

Post 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:
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: mail() working sporadically

Post 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 ..."
Loougle
Forum Newbie
Posts: 22
Joined: Fri Feb 25, 2011 11:23 am

Re: mail() working sporadically

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: mail() working sporadically

Post 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.
Post Reply