无法使用CodeIgniter发送电子邮件

时间:2013-05-24 02:20:57

标签: php codeigniter-2

我正在尝试使用PHP CodeIgniter框架发送电子邮件。在我将该文件上传到我的浏览器后,它没有显示任何错误。它说:“您的电子邮件已成功发送”。但是我的电子邮件帐户中没有收到任何电子邮件。我无法弄清楚问题是什么。我正在使用CodeIgniter版本2.1.3。谁能帮帮我吗。我是PHP新手。谢谢。 这是我的代码:

<?php
class Email extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    function index()
    {
        $this->load->library('email');
        $this->email->from('hasib32@gmail.com', 'Hasan Hasibul');
        $this->email->to('riar32@gmail.com');
        $this->email->subject('email test');
        $this->email->message('testing the email class. email sent');
        if($this->email->send()){
            echo"Your email was sent successfully";
        }else
        {
            show_error($this->email->print_debugger());
        }
    }
}

3 个答案:

答案 0 :(得分:3)

你可以试试这个:

$config = Array(
'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '...@gmail.com',
        'smtp_pass' => '....',
        'mailtype'  => 'html', 
        'charset' => 'utf-8',
        'wordwrap' => TRUE

    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $email_setting  = array('mailtype'=>'html');
    $this->email->initialize($email_setting);
    $email_body ="<div>hello world</div>";
    $this->email->from('...@gmail.com', 'shahriar');

    $list = array('...@gmail.com');
    $this->email->to($list);
    $this->email->subject('Testing Email');
    $this->email->message($email_body);

    $this->email->send();
    echo $this->email->print_debugger();

    }
这对我有用。快乐的编码:)

答案 1 :(得分:1)

这是因为您的localhost中没有邮件服务器设置。您可以设置它,也可以使用您的Gmail帐户发送您的邮件 -

$config = Array(
      ‘protocol’ => ‘smtp’,
      ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
      ‘smtp_port’ => 465,
      ‘smtp_user’ => ‘myusername@gmail.com’,
      ‘smtp_pass’ => ‘mypassword’,
    );

$this->load->library('email', $config);
$this->email->from('hasib32@gmail.com', 'Hasan Hasibul');
$this->email->to('riar32@gmail.com');
$this->email->subject('email test');
$this->email->message('testing the email class. email sent');
if($this->email->send()){
    echo"Your email was sent successfully";
} else {
    show_error($this->email->print_debugger());
}

答案 2 :(得分:0)

检查邮件队列中的电子邮件服务器。可能在等待。