-2

Hii guys i am trying to add contact Us form in my website but its shows

Failed to authenticate username. Error: 535 Incorrect authentication data Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

$config = array(
'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
'smtp_host' => 'mail.31its.com', 
'smtp_port' => 2525,
'smtp_user' => 'dileep@xxx.com
',
'smtp_pass' => 'xxxx',
'smtp_crypto' => 'tls', //can be 'ssl' or 'tls' for example
'mailtype' => 'text', //plaintext 'text' mails or 'html'
'smtp_timeout' => '4', //in seconds
'charset' => 'iso-8859-1',
'wordwrap' => TRUE,

);

Controller

function send() {
    $this->load->config('email');
    $this->load->library('email');

    $from = $this->config->item('smtp_user');
    $to = $this->input->post('to');
    $name = $this->input->post('name');
    $subject = $this->input->post('subject');
    $message = $this->input->post('message');

    $this->email->set_newline("\r\n");
    $this->email->from($from);
    $this->email->to($to);

    $this->email->subject($subject);
    $this->email->message($message);

    if ($this->email->send()) {
        echo 'Your Email has successfully been sent.';
        $this->load->view('Index');
    } else {
        show_error($this->email->print_debugger());
    }
}

Views

<form method="post" action="<?=base_url('index.php/email')?>" enctype="multipart/form-data">
            <div class="form-group">
                <label for="exampleInputText">Name</label>
                <input type="text" id="name" class="form-control"  required="required">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">Subject</label>
                <input type="text" id="subject" class="form-control"  required="required">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">Email</label>
                <input type="email" id="to" class="form-control"  required="required">
            </div>

            <div class="form-group">
                <label for="exampleInputMessage">Message</label>
                <textarea class="form-control" id="message" rows="3"  required="required"></textarea>
            </div>
            <button type="submit" value="Send Email" href="#" class="submit-buttom"><i class="fa fa-2x fa-send" style="color:#eb2526"></i></button>
        </form>
Amit Kumar PRO
  • 1,222
  • 2
  • 15
  • 27

1 Answers1

0

Step 1 : Download PhpMailer for CodeIgniter from below link. https://github.com/ivantcholakov/codeigniter-phpmailer

Step 2 : Extract. Put third_party, libraries, helpers and config folder into your CI application folder.

There will just index file(s) in each folder that will ask you to replace. Click replace and continue.

Step 3 :

Open application/config/email.php

And do some updates according to your email account. I am using gmail, so I am giving gmail settings as below.

$config['protocol']         = 'smtp'; // 'mail', 'sendmail', or 'smtp'
$config['mailpath']         = '/usr/sbin/sendmail';
$config['smtp_host']        = 'smtp.gmail.com'; // if you are using gmail
$config['smtp_user']        = 'youremail@gmail.com';
$config['smtp_pass']        = 'sdkfjsk089sdfskKJ'; // App specific password
$config['smtp_port']        = 465; // for gmail
$config['smtp_timeout']     = 5;  

Step 4 : Now, in your controller where you want to send email. Use below code and its all done.

$this->load->library('email');
$this->email->from('youremail@gmail.com')
     ->reply_to('youremail@gmail.com')
     ->to('someone@somedomain.com')
     ->subject("Subject")
     ->message("Your Message")
     ->set_mailtype('html')
     ->send();
Amit Kumar PRO
  • 1,222
  • 2
  • 15
  • 27
  • $config['mailpath'] = '/usr/sbin/sendmail'; // for what purpose this code is added – 31 IT Solutions Thiruvalla Aug 03 '19 at 09:56
  • $config['mailpath'] = '/usr/sbin/sendmail'; //This is the sendmail path. – Amit Kumar PRO Aug 03 '19 at 10:12
  • You haven't added a recipient address. You need to do this: `$this->email->to('someone@somedomain.com')` Take a look at PHPMailer's example: https://github.com/ivantcholakov/codeigniter-phpmailer – Amit Kumar PRO Aug 03 '19 at 10:17
  • Could not execute: /usr/sbin/sendmail Could not execute: /usr/sbin/sendmail – 31 IT Solutions Thiruvalla Aug 03 '19 at 10:21
  • Also please change the SMTP port according to your server. `Port Number : 465` is for gmail. – Amit Kumar PRO Aug 03 '19 at 10:22
  • Check the permissions of `/usr/sbin/sendmail` – Amit Kumar PRO Aug 03 '19 at 10:28
  • now its shows like this--: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – 31 IT Solutions Thiruvalla Aug 03 '19 at 10:32
  • If you are using default SMTP server then remove email.php from the config and just write following code in your controller or the place where you are calling/sending email. `$this->load->library('email'); $this->email->from('youremail@somedomain.com') ->reply_to('youremail@somedomain.com') ->to('someone@somedomain.com') ->subject("Subject") ->message("Your Message") ->set_mailtype('html') ->send();` – Amit Kumar PRO Aug 03 '19 at 10:47