CodeIgniter发送电子邮件

时间:2013-01-24 08:43:26

标签: php codeigniter email

我有一些问题,这意味着我没有收到来自表格的邮件......

代码如下所示:

public function send_email(){
        $this->load->library("form_validation");


        $this->form_validation->set_rules("fullName", "Full Name", "required|alpha|xss_clean");
        $this->form_validation->set_rules("email", "Email Address", "required|valid_email|xss_clean");
        $this->form_validation->set_rules("message", "Message", "required|xss_clean");

            if($this->form_validation->run() == FALSE){
                $data["message"] = "";
                $this->load->view("site_header");
                $this->load->view("site_nav");
                $this->load->view("content_comment", $data);
                $this->load->view("site_footer");
            }else{
                $data["message"] = "The email was successfully been sent!";


                $this->load->library("email");


                $this->email->from(set_value("email"), set_value("fullName"));
                $this->email->to("paulharbuz@gmail.com");
                $this->email->subject("Message from our form");
                $this->email->message(set_value("message"));

                $this->email->send();

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


                $this->load->view("site_header");
                $this->load->view("site_nav");
                $this->load->view("content_comment", $data);
                $this->load->view("site_footer");
            }
    }

我得到的错误如下:

  

无法使用PHP mail()发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。**

这是来自我的php.ini文件吗?

我在http://php.net/manual/en/ref.mail.php

上设置了阅读此部分的文件

我找到了解决方案:在config:email.php中创建一个看起来像这样的文件

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| EMAIL CONFING
| -------------------------------------------------------------------
| Configuration of outgoing mail server.
| */

$config['wordwrap'] = FALSE;
$config['mailtype'] = 'html';
$config['crlf'] = '\r\n';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['priority']=1;
$config['protocol']='smtp';
$config['smtp_host']='ssl://smtp.googlemail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='gcc.sandbox@gmail.com';
$config['smtp_pass']='qwaszx12';
$config['newline']="\r\n";

/* End of file email.php */
/* Location: ./system/application/config/email.php */

然后在主控制器中加载它,如:$this->email->initialize($this->config->config);就是这样!

3 个答案:

答案 0 :(得分:0)

$string = 'mail content';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: z' . "\r\n";
mail(email, 'subject', $string, $headers);

答案 1 :(得分:0)

只需将此代码添加到您的帮助文件中,并在您要发送邮件时调用该函数以下方法发送电子邮件...

 function send_mail($to,$subject,$message) {
    $config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxx@xxxx',
        'smtp_pass' => 'xxxx',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $this->load->library('email',$config);
    $this->email->set_newline("\r\n");

    $this->email->from($config['smtp_user'],"Your name");
    $this->email->to($to);
    $this->email->subject($subject);  
    $this->email->message($message);
    $result = $this->email->send();
    return $result;
}

答案 2 :(得分:0)

如果您使用的是Gmail,我想在其中添加更多知识,因此无法通过您自己的密码来完成,可以使用特定的Google密码来完成。如果你得到错误,如需要特定于应用程序的密码。 比在谷歌搜索并得到一个并输入下面的字段,就像我一样。 $ config ['smtp_pass'] ='应用专用密码';