4

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: my.news.app@gmail.com : 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: my.news.app@gmail.com : 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

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
stackoverflownewbie
  • 113
  • 1
  • 1
  • 11

2 Answers2

6

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('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');
$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' => 'myself@gmx.com',
        '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('myself@gmx.com', '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 myself@gmx.com and PASSWORD with your own credentials.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55
  • PHPMailer not working.You mentioned about sendmail program.Can you please tell me how to install and configure sendmail in Codeginiter.Help will be greatly appreciated – stackoverflownewbie May 31 '16 at 10:11
  • @stackoverflownewbie `sendmail` is not a feature of CodeIgniter, but its a feature of linux. If `sendmail` is installed on your linux server, CI will call it automatically. To check whether its installed or not, just go to your linux command line and type `sendmail`. If it is not installed, you can do something like `sudo apt-get install sendmail` on ubuntu server. – Prahlad Yeri May 31 '16 at 10:29
  • Refer to [this](http://stackoverflow.com/questions/10359437/sendmail-how-to-configure-sendmail-on-ubuntu) answer too as it might help you configuring sendmail. – Prahlad Yeri May 31 '16 at 10:29
  • Thanks for your prompt reply.But I am on windows and not good with Linux.I think I successfully integrated PHPMailer with Codeigniter but when I try to send the email I get an error(Please see my edited question above for the error).As you can see it says authentication failed.Any solution to this problem(I have allowed access for less secured app and turned off two step verification in my gmail settings). – stackoverflownewbie May 31 '16 at 11:05
  • @stackoverflownewbie Firstly, for future reference, don't edit your entire post to something else. Instead, place an **edit** statement at the end and continue from there, so that any new person can understand exactly what's going on and understand your issue. – Prahlad Yeri May 31 '16 at 21:13
  • Secondly, what you are facing is SMTP authentication error. Are you setting `$mail->Username` and `$mail->Password` correctly? Posting that code might help. Also refer to [this](http://stackoverflow.com/questions/21937586/phpmailer-smtp-error-password-command-failed-when-send-mail-from-my-server) post for guidance. – Prahlad Yeri May 31 '16 at 21:14
  • Thank you bro.Its finally working now.I was writing $mail->UserName instead of $mail->Username and that was what causing the problem.Thank you once again for you valuable guidance and not to mention your valuable time. – stackoverflownewbie Jun 01 '16 at 08:02
2

Download the phpmailer file, move it into application/libraries, then, in you controller o library need to use like (controller or library are the same, only controller extends CI_Controller and library no) controller.php

<?php
include 'PhpMailer.php';

class Test extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }

    public function do_somthing(){
        $this->load->model('model_do');

        $data['value'] = $this->model_do->get_values();

        $view = $this->load->view('view', $data, TRUE);

        SendMail("Header", 'from@mail.com', 'Header 2', 'to@mail.com', 'Subject', $view);
    }
}
elddenmedio
  • 1,030
  • 1
  • 8
  • 15
  • Thanks for your prompt reply.I think I successfully integrated PHPMailer with Codeigniter but when I try to send the email I get an error(Please see my edited question above for the error).As you can see it says authentication failed.Any solution to this problem(I have allowed access for less secured app and turned off two step verification in my gmail settings). – stackoverflownewbie May 31 '16 at 11:06