从网站上的CodeIgniter中使用SMTP gmail发送电子邮件

时间:2017-12-04 07:36:00

标签: php codeigniter email

我在使用codeigniter发送电子邮件时遇到了问题。

我已经成功发送了gmail,但问题是收件人和发件人是一样的。我想从网站发件人那里收到这封电子邮件,

我的控制器

public function index(){        
    $this->sendEmail(); 
    $this->load->view('content_contact');
}
public function sendEmail(){
    $this->load->helper(array('form', 'url'));
    $this->load->library('email');

    //konfigurasi email
    $config = array();
    $config['charset'] = 'utf-8';
    $config['useragent'] = 'Codeigniter';
    $config['protocol']= "smtp";
    $config['mailtype']= "html";
    $config['smtp_host']= "ssl://smtp.gmail.com";
    $config['smtp_port']= 465;
    $config['smtp_timeout']= "5";
    $config['smtp_user']= "my_email$gmail.com";
    $config['smtp_pass']= "my_pass";            
    $config['crlf']="\r\n";
    $config['newline']="\r\n";

    $config['wordwrap'] = TRUE;
    $this->load->view('content_contact');

    $this->email->initialize($config);
    $this->email->from($this->input->post('from'));
    $this->email->to("my_email@gmail.com");
    $this->email->subject($this->input->post('subject'));
    $this->email->message($this->input->post('isi'));

    if($this->email->send()){
        echo "pengiriman email Success";
    }
    else{
        echo "pengiriman email Failed";
    }

}

这是我的查看content_contact.php content_contact.php

1 个答案:

答案 0 :(得分:1)

使用下面提到的解决方案

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'my_email@gmail.com',
    'smtp_pass' => 'yourpassword',
    'mailtype'  => 'html', 
    'charset'   => 'utf-8',
    'wordwrap'  => TRUE
);
$this->load->library('email', $config);