CodeIgniter:server中的SMTP邮件错误可能未配置为发送邮件

时间:2016-12-12 07:13:12

标签: php codeigniter email smtp

我正在尝试使用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());}

2 个答案:

答案 0 :(得分:0)

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

<强> $这 - &GT;负载&GT;辅助(阵列( '电子邮件')); $这 - &GT;负载&GT;库(阵列( '电子邮件'));

<?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());
        }

    }

}

?>

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

答案 1 :(得分:0)

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());
    

}