Search code examples
phpphpmailerswiftmailer

SwiftMailer Connection could not be established with host smtp.mailgun.org


If i use

$transport = Swift_SmtpTransport::newInstance('localhost', 25);

then it's working fine
but when i'm using this settings it's not working

$transport = Swift_SmtpTransport::newInstance('smtp.mailgun.org', 465, 'ssl')
  ->setUsername('mailegun username')
  ->setPassword('mailgun password');

This problem is in "godaddy" and "bluehost" account

but in localhost using xampp it's working fine

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    require_once __dir__.'/vendor/swiftmailer/swiftmailer/lib/swift_required.php';
    $userBody="user mail";
    $userSub = "user subject";
    //$transport = Swift_SmtpTransport::newInstance('localhost', 25);
    $transport = Swift_SmtpTransport::newInstance('smtp.mailgun.org', 465, 'ssl')
      ->setUsername('mailegun username')
      ->setPassword('mailgun password'); 
// and also not working for gmail settings
// $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
   //   ->setUsername('gmail username')
     // ->setPassword('gmail password');


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

    $message = Swift_Message::newInstance('Wonderful Subject')
          ->setFrom(array('[email protected]' => 'user1'))
          ->setTo(array('[email protected]'=> 'user2'))
          ->setBody('Test Message Body')
        ;

    $sendUserMail = $userMailer->send($message,$F);
    var_dump($sendUserMail);


    ?>

Solution

  • This is a well-known problem: GoDaddy does not allow outbound SMTP; you must relay through thir servers. This is covered in the PHPMailer troubleshooting guide, the principles of which still apply to SwiftMailer.