首页 > 解决方案 > CodeIgniter 无法在 VPS 上发送电子邮件

问题描述

我目前正在使用 CodeIgniter 开发 REST API。我得到了一个使用 gmail smtp 的电子邮件验证系统,在 localhost 上工作得很好。但是当我将它上传到 VPS 服务器时,它不起作用。

这是我的代码:

protected function send_confirmation($email, $hash){
    $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxx@gmail.com',
        'smtp_pass' => 'xxx',
        'mailtype'  => 'html',
        'charset'   => 'utf-8',
        'crlf' => "\r\n",
        'newline' => "\r\n"
    );

    $this->load->library('email', $config);

    $mail = $this->email;

    $mail->from('no-reply@example.com', 'Example.com');
    $mail->to($email);

    $mail->subject('Email Verification');

    $encodemail = urlencode($email);
    $message = 'Some messages here.';

    $mail->message($message);   

    $mail->send();
}

另请注意,我使用的是PHP 7.2,并且尝试了许多配置,但在 VPS 服务器上似乎没有任何效果。

标签: phpcodeigniter-3

解决方案


试试这些

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.gmail.com', #changed
    'smtp_port' => 587, #changed 
    'smtp_user' => 'xxx@gmail.com',
    'smtp_pass' => 'xxx',
    'mailtype'  => 'html',
    'charset'   => 'utf-8',
    'newline' => "\r\n"
);

或使用PHPMailer


推荐阅读