【问题标题】:how to send email using codeigniter如何使用 codeigniter 发送电子邮件
【发布时间】:2015-09-06 04:24:33
【问题描述】:

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

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

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

并在我的本地主机中打开 php.extension->php.openssl enable 以及 465 的端口号更改和 smtp:smtp.gmail.com 这都是启用它。

但不工作帮助我。?

【问题讨论】:

标签: codeigniter


【解决方案1】:

试试这个代码。这是工作代码和 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');
} 

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 嗨,anam shah,此代码不起作用。因为出现此错误
    • 发件人: 返回路径: 回复:“sivaprocessdrive@gmail.com” X 发件人: sivaprocessdrive@gmail.com X-Mailer: CodeIgniter X-Priority: 3 (Normal) Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit =?utf-8?Q?Resume_from_siva?= 你的邮件正文在这里
    • 评论最后一行
    • echo $this->email->print_debugger();...然后检查您的邮件 ....此行打印电子邮件的调试信息
    • @sivakumar.s 你能提供网站网址吗
    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 2019-08-07
    • 1970-01-01
    • 2015-11-10
    相关资源
    最近更新 更多