2

I'm using codeigniter I created one email function to send email to particular email ID

public function email($email){
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.gmail.com';
        $config['smtp_timeout']=5;
        $config['smtp_port'] = '465';
        $config['smtp_user'] = 'MyEmailID@gmail.com';
        $config['smtp_pass'] = '********************';
        $config['mailtype'] = 'html';
        $config['charset'] = 'utf-8';
        $config['newline'] = "\r\n";
        $config['wordwrap'] = TRUE;
        $this->load->library('email');
        $this->email->initialize($config);

        $this->email->from('MyEmailID@gmail.com', 'MyEmailID');
        $this->email->to($email);
        $this->email->subject('My Subject');
        $this->email->message('My Message');
        return $this->email->send();
    }

That's working nice in my local machine but when I uploaded that code to hosting server (I'm using Godaddy hosting) I'm getting following error:

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

Filename: libraries/Email.php

Line Number: 2020

Backtrace:

File: /home/MyDir/public_html/application/models/BasicModel.php Line: 47 Function: send

File: /home/MyDir/public_html/application/controllers/Login.php Line: 48 Function: email

File: /home/MyDir/public_html/index.php Line: 315 Function: require_once

Abhijit Kumbhar
  • 923
  • 3
  • 23
  • 49

4 Answers4

2

Use Tls istead of ssl and change port number to 587 $this->email->initialize($config); $config = array();

     //Load email library 
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'tls://smtp.gmail.com';
        $config['smtp_port']    = '587';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 'email';
        $config['smtp_pass']    = 'password';
        $config['charset']    = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $config['validation'] = TRUE; // bool whether to validate email or not      



            $this->email->set_newline("\r\n");
            ;
            $this->email->clear(TRUE);
            $this->email->to($this->input->post('email'));
            $this->email->subject($row_email->email_subject);

working site

Yasir Ali
  • 21
  • 6
0

This kind of error was in my case caused by a source server firewall blocking the the SMTP connection.

It can help to unblock/whitelist destination IP or disable SMTP blocking/whitelist user in ConfigServerFirewall (if used) configuration:

SMTP_BLOCK = off
SMTP_ALLOWUSER = usernamehere
16851556
  • 255
  • 3
  • 11
-1

You forgot to add '

change from

 $config['smtp_host'] = 'ssl://smtp.gmail.com';

to

$config['smtp_host'] = 'smtp.gmail.com';

or

Uncomment php_openssl.dll in php.ini

dhruv jadia
  • 1,684
  • 2
  • 15
  • 28
-1

Change from:

$config['smtp_host'] = 'smtp.seuDominio.com';

To:

$config['smtp_host'] = 'mail.seuDominio.com';
$config['smtp_port'] = 587; //587
$config['smtp_user'] = 'seu@dominio.com';
$config['smtp_pass'] = 'suaSenha';

Change from:

$config['protocol']  = 'smtp'; 

To:

$config['protocol']  = 'mail';
$config['validate']  = TRUE;
$config['mailtype']  = 'html';
$config['charset']   = 'utf-8';
$config['newline']   = "\r\n";
prosoitos
  • 6,679
  • 5
  • 27
  • 41
  • Please add some explanation to your answer such that others can learn from it. The original code does not contain any occurence of `smtp.seuDominio.com` – Nico Haase Oct 09 '22 at 16:17