I cannot send any emails using Codeigniter

2020-07-17 15:31发布

I have created a controller and made a test function in the controller to test if the email is sent or not.

I checked with different email addresses, but didn't succeed. This is my code example:

public function sendmail() {
    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $user_id = 1;
    $name = 'Mark Alan';
    $this->email->set_newline("\r\n");
    $this->email->from('info@domainname.com', "Test Name");
    $this->email->to('test@domainname.com');
    $this->email->subject('Test Mail');
    $this->email->message('This is a test message');
    $this->email->send();
    echo $this->email->print_debugger();    
}

2条回答
Evening l夕情丶
2楼-- · 2020-07-17 15:36

Try this code:

function sendMail()
{
    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com', //Your Host
        'smtp_port' => 465, // Add 25 port if you sending from your smtp mail server 
        'smtp_user' => 'xxx@gmail.com', // change it to yours, server email
        'smtp_pass' => 'xxx', // change it to yours
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
);

    $message = '';
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('xxx@gmail.com'); // change it to yours
    $this->email->to('xxx@gmail.com');// change it to yours
    $this->email->subject('Resume from JobsBuddy for your Job posting');
    $this->email->message($message);
    if($this->email->send())
    {
        echo 'Email sent.';
    }
    else
    {
        show_error($this->email->print_debugger());
    }

}

Note: Please add your smtp port and your smtp email account detail.

查看更多
劳资没心,怎么记你
3楼-- · 2020-07-17 15:37

With Codeigniter (assuming you have auto-loaded the email library) you can either set an email preference in a config file, named email.php and these preferences are loaded automatically from there. It could look like:

// Setting Email Preferences 
$config['useragent'] = 'CodeIgniter';              // Mail engine switcher: 'CodeIgniter' or could be 'PHPMailer'
$config['protocol']  = 'smtp';                   // 'mail', 'sendmail', or 'smtp'
$config['mailpath']  = '/usr/sbin/sendmail';

or like in your example, you can set them manually, but need to initialize it, so don't forget this line after defining your $config[] array:

$this->email->initialize($config);
查看更多
登录 后发表回答