【问题标题】:SMTP mail error in CodeIgniter : server might not be configured to send mailCodeIgniter 中的 SMTP 邮件错误:服务器可能未配置为发送邮件
【发布时间】:2017-04-27 00:28:21
【问题描述】:

我正在尝试使用 CodeIgniter 发送 SMTP 电子邮件,但我收到错误响应...

"遇到以下 SMTP 错误:0 无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。"

User-Agent: CodeIgniter
Date: Tue, 13 Dec 2016 07:06:56 +0000
From: "Blabla" <xxxxxxxxxxxxxx>
Return-Path: <xxxxxxxxxxxxxxxxxxx>
To: xxxxxxxxxxxx
Reply-To: "Explendid Videos" <xxxxxxxxxxxxxxxxxx>
Subject: =?utf-8?Q?=54=68=69=73=20=69=73=20=61=6E=20=65=6D=61=69=6C=20=74=65=73=74?=
X-Sender: bttvsupport@cloudlink.co.in
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <584f9e1023f2f@xxx.co.in>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_584f9e1023f99"

This is a multi-part message in MIME format.
Your email application may not support this format.

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

It is working. Great!


--B_ALT_584f9e1023f99
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

It is working. Great!

--B_ALT_584f9e1023f99--

代码是

$ci = get_instance();
        $ci->load->library('email');
        $config = array();
        $config['protocol'] = "smtp";
        $config['smtp_host'] = "xxxxx";
        $config['smtp_port'] = 25;
        $config['smtp_user'] = "xxxxx"; 
        $config['smtp_pass'] = "xxxxx";
        $config['charset'] = "utf-8";
        $config['mailtype'] = "html";
        $config['newline'] = "\r\n";

        $ci->email->initialize($config);

        $ci->email->from('xxxxx', 'Blabla');
        $list = array('xxxxx');
        $ci->email->to($list);
        $this->email->reply_to('xxxxx', 'Explendid Videos');
        $ci->email->subject('This is an email test');
        $ci->email->message('It is working. Great!');
        if($ci->email->send()){echo "send";}else{echo "error ";print_r($ci->email->print_debugger());}

【问题讨论】:

  • 使用$this-&gt;ci 而不是$ci
  • 我会把这个 $this->ci = get_instance();在库的 __construct() 区域中
  • 您需要向您的服务提供商咨询您的服务器的 smtp 详细信息。此外,如果您使用 gmail 从您的应用程序发送电子邮件,那么您需要从 gmail-settings 激活 smtp 电子邮件。再说一件事,如果您将应用程序上传到服务器上,则无需使用smtp 协议,而是使用sendmail
  • 您是否加载了电子邮件库?并使用 $this->email->initialize($config);

标签: php codeigniter email smtp


【解决方案1】:
public function send_mails(){

    $config['useragent'] = 'CodeIgniter';
    $config['protocol'] = 'https';
    $config['smtp_host'] = 'smtp.gmail.com';
    $config['smtp_port'] = '587';
    $config['smtp_user'] = 'youremail@gmail.com';
    $config['smtp_pass'] = '*********';
    $config['wrapchars'] = 76;
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    
    $this->load->library('email', $config);

    $from_email = "youremail@gmail.com";
    $to_email = "reseiver@gmail.com";
    //Load email library
    $this->load->library('email');
    $this->email->initialize($config);
    //$this->load->library('email', $config);
    $this->email->from($from_email, 'Identification');
    $this->email->to($to_email);
    $this->email->subject('Send Email Codeigniter');
    $this->email->message('The email send using codeigniter library');
    //Send mail
    if($this->email->send())
       echo "mail sent";
    else
      show_error($this->email->print_debugger());
    

}

【讨论】:

    【解决方案2】:

    我认为您需要在构造函数中加载电子邮件库,例如

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

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Dispatch extends MY_Controller 
    {
    
            function __construct() {
                            parent::__construct();
                            $this->load->helper('url');
                            $this->load->helper(array('form', 'url'));
                            $this->load->library('form_validation');
                            $this->load->library('session');
                            $this->load->helper(array('email'));
                            $this->load->library(array('email'));
    
            }
    
    public function sendEmail()
        {
    $config = array(
        'protocol'  => 'smtp',
        'smtp_host' => 'smtp.googlemail.com',
        'smtp_port' => '587',
        'smtp_user' => 'test@gmail.com',
        'smtp_pass' => 'XXXXXXX',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->set_mailtype("html");
    $message2 = '
        <html>
            <head>
                <title>Dispatch</title>
            </head>
            <body>
    
    
    
    
    
    
            Test Mail
    
    
    
    
            </body>
        </html>
        ';
             $subject="Test Email";
             $message=$message2;
              $this->email->from('test@gmail.com');
              $this->email->to($test@gmail.com);
              $this->email->subject($subject);
              $this->email->message($message);
              if($this->email->send())
             {
                     echo "<script type='text/javascript'>alert('Email Send Success');</script>";
                     redirect('/Dispatch/', 'refresh');
             }
             else
            {
             show_error($this->email->print_debugger());
            }
    
        }
    
    }
    
    ?>
    

    我也面临同样的问题,但是当我尝试在构造函数中加载库时,电子邮件发送成功。这对我有用,希望这对你也有帮助。

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 2019-02-23
      • 2022-01-05
      • 2019-06-27
      • 1970-01-01
      • 2017-12-28
      • 2018-12-08
      • 2018-09-20
      相关资源
      最近更新 更多