使用SMTP codeigniter发送电子邮件

时间:2015-08-18 06:36:22

标签: php codeigniter email smtp

我正在尝试使用smtp codeigniter发送电子邮件。我使用的代码如下:

public function notify_marketing(){
    $config = Array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'ssl://smtp.googlemail.com',
                    'smtp_port' => 465,
                    'smtp_user' => 'myvalidemail@gmail.com', 
                    'smtp_pass' => '*******',//my valid email password
                    'mailtype' => 'html',
                    'charset' => 'iso-8859-1',
                    'wordwrap' => TRUE
                  );

    $this->email->initialize($config);
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");  
    $this->email->from('myvalidemail@gmail.com'); 
    $this->email->to('validreceiptent@gmail.com');
    $this->email->subject('My Subject');
    $this->email->message('Hello there');
    if($this->email->send())
    {
        $this->session->set_flashdata("success","Email sent.");
    }
     else
    {
        show_error($this->email->print_debugger());
    }
}

但是我在响应中收到以下错误。我尝试过这个网站的其他解决方案但是没有用。

    <div id="exception_error">
    <h1><span class="type">An Error Was Encountered [ 500 ]</span></h1>
    <div class="content">
        <p><p>220 smtp.googlemail.com ESMTP bv4sm16669443pbb.86 - gsmtp
<br /><pre>hello: 250-smtp.googlemail.com at your service, [110.44.127.179]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
</pre>lang:email_smtp_auth_pw<br />lang:email_send_failure_smtp<br /><pre>User-Agent: CodeIgniter
Date: Tue, 18 Aug 2015 12:03:42 +0545
From: <********@gmail.com>
Return-Path: <********@gmail.com>
To: *******@gmail.com
Subject: =?iso-8859-1?Q?My_Subject?=
Reply-To: "********@gmail.com" <*********@gmail.com>
X-Sender: *******@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <55d2ce42747f5@gmail.com>
Mime-Version: 1.0

提前谢谢。

2 个答案:

答案 0 :(得分:4)

代码很完美。

您需要使用gmail的应用程序密码而不是gmail密码。请从Gmail设置中设置应用程序密码,并在此处输入密码。

它会完美地运作。查看HERE

的更多信息

在gmail中设置应用密码的过程:

  1. 访问App passwords page。系统可能会要求您登录Google帐户。
  2. 在底部,点击选择应用,然后选择您正在使用的应用。
  3. 单击选择设备,然后选择您正在使用的设备。
  4. 选择生成。
  5. 按照说明在设备上输入应用程序密码(黄色栏中的16个字符代码)。
  6. 选择完成。
  7. 完成后,您将不会再次看到该应用密码。但是,您将看到您为其创建应用密码的应用和设备列表。

答案 1 :(得分:0)

试试这个..

创建一个名为&#39; email.php&#39;的文件。内部&#39; application / config&#39;文件夹并将以下设置添加到其中。

<?php
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'user@gmail.com'; //change this
    $config['smtp_pass'] = 'password'; //change this
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n";
?>

在控制器中:

<?php
//send mail
function sendmail()
{
    $this->load->library('email'); // load email library
    $this->email->from('user@gmail.com', 'sender name');
    $this->email->to('test1@gmail.com');
    $this->email->cc('test2@gmail.com'); 
    $this->email->subject('Your Subject');
    $this->email->message('Your Message');
    $this->email->attach('/path/to/file1.png'); // attach file
    $this->email->attach('/path/to/file2.pdf');
    if ($this->email->send())
        echo "Mail Sent!";
    else
        echo "There is error in sending mail!";
}
?>