Codeigniter: SMTP mail not working

2019-02-15 22:29发布

问题:

I'm trying to set up a contact form, and I'm struggling to get it working with the SMTP configuration in CI. However I've successfully tried with the basic 'mail' config.

To cut it short, I'm doing my tests with this simple code:

    $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'smtp.1and1.es',
        'smtp_port' => 25,
        'smtp_user' => 'user@mysite.com', 
        'smtp_pass' => '********',
        'mailtype'  => 'text',
        'charset'   => 'utf-8',
        'wordwrap'  => true,
        'wrapchars' => 50            
    );

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

    $this->email->from('my_real_email_address@gmail.com');
    $this->email->to('my_real_email_address@gmail.com');
    $this->email->reply_to('my_real_email_address@gmail.com', 'John Doe');
    $this->email->subject('Message sent from the contact form in website');
    $this->email->message('Body of message...');

    if ($this->email->send()) {
            return true;
    }
    echo '<!--' . print_r($this->email->print_debugger(), true) . '-->'; 
    return false;

I've just asked my hosting provider and all the SMTP config is correct. They've also checked my e-mail account is working properly.

In fact if I choose:

protocol => 'mail'

the message is delivered without problems. AFAIK the php mail() function also uses the hosting provider SMTP but it simply hands it over to the server and forgets it.

Instead I'd like to use SMTP authentication, which worries about the dispatch of the e-mail. But only by changing

protocol => 'smtp'

when I send the form there's a lot of processing time and I finally get this debugging message:

220 smtp.1and1.es (mreu2) Welcome to Nemesis ESMTP server

hello:  The following SMTP error was encountered:
Failed to send AUTH LOGIN command. Error: 421 smtp.1and1.es connection timed out

from:  The following SMTP error was encountered:
to:  The following SMTP error was encountered: 
data:  The following SMTP error was encountered:  

The following SMTP error was encountered: 
Unable to send email using PHP SMTP.  Your server might not be configured to send mail using this method.
User-Agent: CodeIgniter
Date: Thu, 24 Jan 2013 18:51:31 +0100
From: <my_real_email_address@gmail.com>
Return-Path: <my_real_email_address@gmail.com>
To: my_real_email_address@gmail.com
Reply-To: "John Doe" <my_real_email_address@gmail.com>
Subject: =?utf-8?Q?Message_sent_from_the_contact_form_in_website?=
X-Sender: my_real_email_address@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <510174a33158d@gmail.com>
Mime-Version: 1.0


Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Body of message...

What might be the reason of this error?

回答1:

This guy mentioned having an issue with a config item having single quotes:

$config['crlf'] = '\r\n';      //should be "\r\n"
$config['newline'] = '\r\n';   //should be "\r\n"

I'm pretty certain protocol => mail does not use your provider -- it will send the email directly from your server.

If it's not the single quote issue, it might be an issue with incorrect credentials, firewall or connection requirement (say if your email provider requires TLS/SSL).

Try connecting to the mail server and send an email with another client (you could use telnet: http://www.wikihow.com/Send-Email-Using-Telnet or thunderbird or somesuch) to verify the protocols are correct.