Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PHPMailer in codeigniter?

I get the following error when I try to send email using PHPMailer



2016-05-31 10:44:55 CLIENT -> SERVER: EHLO localhost 2016-05-31 10:44:56 CLIENT -> SERVER: MAIL FROM: 2016-05-31 10:44:56 SMTP ERROR: MAIL FROM command failed: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 v27sm39349082pfi.49 - gsmtp 2016-05-31 10:44:56 The following From address failed: [email protected] : MAIL FROM command failed,Authentication Required. Learn more at https://support.google.com/mail/answer/14257 v27sm39349082pfi.49 - gsmtp ,530,5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/answer/14257 v27sm39349082pfi.49 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1 The following From address failed: [email protected] : MAIL FROM command failed,Authentication Required. Learn more at https://support.google.com/mail/answer/14257 v27sm39349082pfi.49 - gsmtp ,530,5.5.1SMTP server error: MAIL FROM command failed Detail: Authentication Required. Learn more at https://support.google.com/mail/answer/14257 v27sm39349082pfi.49 - gsmtp SMTP code: 530 Additional SMTP info: 5.5.1 2016-05-31 10:44:56 CLIENT -> SERVER: QUIT

like image 318
stackoverflownewbie Avatar asked Feb 07 '23 22:02

stackoverflownewbie


1 Answers

First of all, you need to ensure that sendmail program is installed on your lamp server in order for mails to be sent from the CI3 Email class. On your localhost, you most probably won't be having this installed, but on most web hosting servers, it will be. So my first suggestion is to try and run it from your remote web server and see whether the mail is sent. Make sure that you load the email library before sending the mail from your controller. Something like this:

//run this from your controller
$this->load->library('email');
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->cc('[email protected]');
$this->email->bcc('[email protected]');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();

If you want more control over it, CI3 also provides you configuration options for that. You can configure sendmail path and other variables as follows before sending the mail. Here is the complete list of preferences that you may set:

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);

However, if you still insist on using PHPMailer, then you can do as elddenmedio suggests. However, in that case, its better to put the PHPMailer inside the library or third_party folder and loading from there in the constructor instead of using require every now and then.

Edit:

In case, someone finds this through a google search, here is the complete code to send a mail using smtp which I had used in a recent CI3 project. This is for sending mail without using PHPMailer library:

public function send_email($to, $subject, $message) {
    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'mail.gmx.com',
        'smtp_port' => 587, //465,
        'smtp_user' => '[email protected]',
        'smtp_pass' => 'PASSWORD',
        'smtp_crypto' => 'tls',
        'smtp_timeout' => '20',
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $config['newline'] = "\r\n";
    $config['crlf'] = "\r\n";
    $this->CI->load->library('email', $config);
    $this->CI->email->from('[email protected]', 'Admin');
    $this->CI->email->to($to);
    $this->CI->email->subject($subject);
    $this->CI->email->message($message);

    //$this->email->send();
    if ( ! $this->CI->email->send()) {
        return false;
    }
    return true;
}

Just replace the [email protected] and PASSWORD with your own credentials.

like image 188
Prahlad Yeri Avatar answered Feb 10 '23 10:02

Prahlad Yeri