Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HostGator Won't Send Mail via PHP SwiftMail API

Is there a reason why SwiftMail API for PHP won't run on HostGator? Their tech support said use the mail() command and that socket connections to port 25 won't work like that on their shared hosting plan. I complained and said that when you google on how to send attachments with mail(), people tell you to use SwiftMail. I need to send file attachments with the email.

My code works just fine on A2Hosting, but not HostGator. In the API, I'm sending to localhost on port 25, using no authentication, using no SSL, and using a valid email address that does exist on this host (pretend it's example.com) -- [email protected].

The error I'm getting is:

Warning: fsockopen() [function.fsockopen]: unable to connect to localhost:25 (Connection refused) in /home/user/public_html/mywebsite/addons/swiftmail-4.0.3/lib/classes/Swift/Transport/StreamBuffer.php on line 243

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host localhost [Connection refused #111]' in /home/user/public_html/mywebsite/addons/swiftmail-4.0.3/lib/classes/Swift/Transport/StreamBuffer.php:245 Stack trace: #0 /home/user/public_html/mywebsite/addons/swiftmail-4.0.3/lib/classes/Swift/Transport/StreamBuffer.php(80): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /home/user/public_html/mywebsite/addons/swiftmail-4.0.3/lib/classes/Swift/Transport/AbstractSmtpTransport.php(111): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/user/public_html/mywebsite/addons/swiftmail-4.0.3/lib/classes/Swift/Mailer.php(84): Swift_Transport_AbstractSmtpTransport->start() #3 /home/user/public_html/mywebsite/mailer.php(34): Swift_Mailer->send(Object(Swift_Message)) #4 /home/user/public_html/mywebsite/mailer.php(146): Mailer->sendMail() #5 {main} thrown in /home/user/public_html/mywebsite/addons/swiftmail-4.0.3/lib/classes/Swift/Transport/StreamBuffer.php on line 245

If you've gotten SwiftMail API to work on HostGator, please share what you did.

like image 683
Volomike Avatar asked Aug 24 '11 04:08

Volomike


2 Answers

You can try using Swift_MailTransport instead of Swift_SmtpTransport which will use the native mail() call behind the scenes.

I have no experience with HostGator but if their support says the system allows sending through mail() this should work fine.

like image 151
Anshul Avatar answered Sep 21 '22 16:09

Anshul


I ran some tests and found the answer finally. It does work, but here's how:

  • use port 25, not an alternative port
  • do not use SSL/TLS
  • using a user/pass is not necessary, but will work with or without it
  • do not use 'localhost' -- use the name of the domain where the mail account exists, whether it's the root domain, addon domain, subdomain, or parked domain
  • ensure that the sending email account exists, and exists in the domain context you want. For instance, I may have example1.com as the root domain, and example2.com as the addon domain for my project. So, in my project code, I'll create a [email protected] account in cPanel and then use this as the sending email address with the SwiftMailer API.
like image 45
Volomike Avatar answered Sep 21 '22 16:09

Volomike