Home >Backend Development >PHP Tutorial >How to Fix 'fsockopen(): unable to connect to ssl://smtp.gmail.com:465' Error When Sending Emails with CodeIgniter?

How to Fix 'fsockopen(): unable to connect to ssl://smtp.gmail.com:465' Error When Sending Emails with CodeIgniter?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-13 18:05:11876browse

How to Fix

Sending Email with Gmail's SMTP Using CodeIgniter's Email Library

This article aims to address a common issue encountered when sending emails using Gmail's SMTP server via CodeIgniter's email library:

Problem:

When using the original configuration (with SSL encryption and port 465), the following error occurs:

"fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection timed out)"

Solution 1:

Replace the original configuration with the enhanced one provided in the answer:

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'xxx',
    'smtp_pass' => 'xxx',
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

// Set to, from, message, etc.

$result = $this->email->send();

Solution 2 (Alternative):

Use a custom helper provided by a CodeIgniter user:

$this->load->helper('email');

$config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.gmail.com',
    'smtp_port' => 465,
    'smtp_user' => 'xxx',
    'smtp_pass' => 'xxx',
    ' mailtype ' => 'html',
    'charset'  => 'iso-8859-1'
);

send_email('toemail@example.com', 'Email Subject', 'Email Body', $config);

The above is the detailed content of How to Fix 'fsockopen(): unable to connect to ssl://smtp.gmail.com:465' Error When Sending Emails with CodeIgniter?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn