2

I am sending email through php using smtp and using Swift_Mailer

Code:

 error_log("Sending Email\n");
    // Get mailer
    $SM = Yii::app()->swiftMailer;

    // Get config
    $mailHost = 'smtp.gmail.com';
    $mailPort = 465; 
    $security = 'ssl';
    // New transport
    $Transport = $SM->smtpTransport($mailHost, $mailPort, $security)
        ->setUsername('username@gmail.com')
        ->setPassword('password');

    // Mailer
    $Mailer = $SM->mailer($Transport);

    // New message
    $Message = $SM
        ->newMessage($subject)
        ->setFrom('username@gmail.com')
        ->setTo($to)
        ->setBody($body);

    // Send mail
    $result = $Mailer->send($Message);

Error:

fsockopen() [function.fsockopen]: >unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

Also

PLEASE NOTE THAT: There is no SSL Certificate attached to my hosting server account. PLEASE NOTE THAT: I am not getting the error at my local computer.

Question

  • Do I need SSL certificates at my hosting account? (need to confirm)
  • if not, then what I am getting wrong in this?
2

You do not need a SSL certificate in order to send a secure email, the same way desktop computers, tablets and mobiles phones don't need one either.

This could be happening because your server doesn't have OpenSSL installed, or your script is at fault. You can check if you have OpenSSL installed by doing: openssl version. If you need to install OpenSSL then do the following:

  • sudo apt-get install openssl
  • sudo apt-get install libssl-dev

If you used shared hosting or another hosting environment that does not allow install of core system applications then you need to contact your web host. If you issue is with the script then Pro Webmasters is not the idea stack for this question and your need to ask on Stack Overflow, since its programming related.

  • ok, can I install it through ssh ? – Omer JR Apr 25 '16 at 8:50
  • I have checked the script. it's working on my local computer and openssl extension is selected. – Omer JR Apr 25 '16 at 8:51
  • Also, I noticed that during several tries sometime it[ php script at server ] do send email but then for the next try it fails. this behavior is really strange. – Omer JR Apr 25 '16 at 8:52
  • Through ssh i checked that openssl is there. when i run this command openssl s_client -connect smtp.gmail.com:465 it returns an error: socket: connection refused connection:errno = 29 – Omer JR Apr 25 '16 at 9:10
  • It needs libssl too, since OpenSSL is useless without the libraries. – Simon Hayter Apr 25 '16 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.