如何使用codeigniter发送电子邮件

时间:2015-06-20 05:55:19

标签: codeigniter

今天我使用代码点火器发送电子邮件但工作不正常。所以请任何人帮助我。

这是我的控制器页面代码;

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {enter code here

	 function __construct() {
	 parent::__construct();
			
			 $this->load->helper(array('form', 'url'));
			  // $this->load->library('pagination');
			$this->load->library('session','form_validation');
		} 	
	 
function sendMail() 
{ 
$config = Array( 
'protocol' => 'smtp', 
'smtp_host' => 'ssl://smtp.gmail.com', 
'smtp_port' => 465, 
'smtp_user' => 'siva@gmail.com', // here goes your mail 
'smtp_pass' => 'mygamilpassword', // here goes your mail password 
'mailtype' => 'html', 
'charset' => 'iso-8859-1', 
'wordwrap' => TRUE 
);
 $this->email->initialize($config);    
$message = 'hiiiiii'; 
$this->load->library('email', $config); 
$this->email->set_newline("rn"); 
$this->email->from('siva@gmail.com'); // here goes your mail 
$this->email->to('sam@gmail.com');// here goes your mail 
$this->email->subject('Resume from JobsBuddy'); 
$this->email->message($message); 
if($this->email->send()) 
{ 
echo 'Email sent.'; 
} 
else 
{ 
show_error($this->email->print_debugger()); 
}
}	$this->sendMail;
	 } 

并在我的localhost中打开php.extension-&gt; php.openssl enable以及465的端口号更改,以及smtp:smtp.gmail.com这都是启用它。

但没有工作帮助我。?

2 个答案:

答案 0 :(得分:1)

试试这段代码。这是工作代码:

function sendMail() 
{ 
    $config['protocol'] = "smtp";
    $config['smtp_host'] = "ssl://smtp.googlemail.com";
    $config['smtp_port'] = "465";
    $config['smtp_user'] = "siva@gmail.com";
    $config['smtp_pass'] = "mygamilpassword";

    $message = "Your email body text goes here";

    $config['mailtype'] = "html";
    $ci = & get_instance();
    $ci->load->library('email', $config);
    $ci->email->set_newline("\r\n");
    $ci->email->from("siva@gmail.com");
    $ci->email->to("siva@gmail.com");
    $ci->email->subject("Resume from JobsBuddy");
    $ci->email->message($message);
    $ci->email->send();  
    echo $this->email->print_debugger();
}

答案 1 :(得分:0)

尝试此代码。这是有效的代码,并且allow-less-secure-apps-access-gmail-account setting open

public function send_mail() {

    ini_set('SMTP', "server.com");
    ini_set('smtp_port', "25");
    ini_set('sendmail_from', "yourmail@gmail.com");

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

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

    $config['protocol']    = 'smtp';
    $config['smtp_host']    = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = 'yourmail@gmail.com';
    $config['smtp_pass']    = 'yourpassword';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'text'; // or html
    $config['validation'] = TRUE; // bool whether to validate email or not
    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

    $from_email = "frommailid@gmail.com";
    $to_email = "tomailid@gmail.com";
    //Load email library
    $this->load->library('email');
    $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())
        $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
    else
        show_error($this->email->print_debugger());
        $this->session->set_flashdata("email_sent","You have encountered an error");
    $this->load->view('contact_email_form');
}