简体   繁体   中英

Swiftmailer: Connection refused #111

I am trying to use swiftmailer to send emails from my website however when I run the php script I get the following error:

Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection refused #111]'

and the code I am using looks like this:

require_once "vendor/swiftmailer/swiftmailer/lib/swift_required.php";

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")

    ->setUsername("EMAIL")

    ->setPassword('PASSWORD');

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

$message = Swift_Message::newInstance('Website Enquiry')

    ->setFrom(array($email))

    ->setTo(array('MY EMAIL'))

    ->setBody($msg);

$result = $mailer->send($message);

echo $result;
return $result;

Any help would be much appriciated. Thanks!

(This is not a duplicate of Swiftmailer: Connection could not be established with host smtp.gmail.com [Connection timed out #110] because of the different code (#110 vs #111) and because I didn't set up swiftmailer using configuration but instead in my code. I'm not sure if that makes sense, but basically the answers in the other question don't solve my question.)

Ok, there isn't any solution. It turns out that GoDaddy (my web host) blocks ports 25 and 465 (and another SMTP one that I cant remember). So the only solution is switching hosts.

GoDaddy may block those ports, but you can use sendmail to send the message. See the example below:

$transport = new Swift_SendmailTransport('/usr/sbin/sendmail -bs');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM