HostGator Won't Send Mail via PHP SwiftMail API
Asked Answered
C

6

5

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.

Clinquant answered 24/8, 2011 at 4:56 Comment(0)
G
13

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.

Glabrate answered 24/8, 2011 at 5:7 Comment(0)
C
4

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.
Clinquant answered 24/8, 2011 at 5:27 Comment(2)
This pretty much will occur on any hosting account. You may be able to debug by sending emails to noreply (with no domain.com on the end), but it won't fly on a hosting account and you'll need to not only add the domain.com, but also go create the mail account.Clinquant
any ideas i am unable to send mail on hostgator using swift mailer but i was able to send mail from localhost using gmail smtp server, i think there is error in $transport = Swift_MailTransport::newInstance(Outgoing Server name, 25); in this line , the output returns false.Byte
C
2

Turns out if you are trying to send to a domain that is in your CPanel, even if you have email hosted somewhere else, their mail server tries to relay the message to a local mailbox instead of consulting the MX records for the domain.

The fix is to tell them to remove the domain you are trying to email from their localhost option in their exim mail server configuration file.

Canakin answered 2/1, 2014 at 3:47 Comment(0)
S
1

I am using SwiftMail for PHP with hostgator with Swift_SmtpTransport connecting to gmail over ssl. At first I was getting a "Connection could not be established with host smtp.gmail.com [Connection refused #111]". Using Swift_MailTransport did work, but I did not like that solution because it would use hostgator's mail server; I wanted to use a google apps email account to send the email.

I had to ask HostGator's tech support to whitelist the cPanel user in the firewall for SMTP, and then everything worked well.

Scooter answered 9/9, 2013 at 12:43 Comment(0)
H
0

using a valid email address that does exist on this host (pretend it's example.com) -- [email protected].

are you sure the mailserver is on the same box? did you tried to dig the mx record of example.com

Horntail answered 24/8, 2011 at 10:6 Comment(0)
K
0

I got this error while trying to send emails using Amazon's Simple Mail Service. Ultimately it turned out to be my credentials for using AmazonSES were wrong. Once i got this correct every thing resolved out for me.

By the way I was using PHP's Swift_SmtpTransport

Kiddush answered 17/9, 2014 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.