Carson
Carson

Reputation: 57

Send email in CodeIgniter

I am newbie in CodeIgniter. May I know how to send an email in CodeIgniter via localhost? What should I put in the smtp_user and smtp_pass? Below is my controller code:

function Forgot_Password()
    {
        $this->form_validation->set_rules('Email', 'Email', 'trim|required|valid_email|callback_email_check');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('Forgot_Password');
            $this->load->view('templates/footer'); 
        }
        else
        {
            $this->load->library('email');


           $config = Array(
               'protocol' => 'smtp',
               'smtp_host' => 'ssl://smtp.googlemail.com',
               'smtp_port' => 465,
               'smtp_user' => 'xxxxxxxxxx',
               'smtp_pass' => 'xxxxxxxxxx'
               );

            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $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.');  

            if($this->email->send())
            {
                echo 'Email sent.';
            }
            else
            {
                show_error($this->email->print_debugger());
            }
        }
     }

In config.php (config folder), I have:

$config['smtp_host'] = 'ssl://smtp.gmail.com'; // SMTP Server Address.
$config['smtp_port'] = '465'; // SMTP Port.
$config['protocol'] ='sendmail';

Upvotes: 1

Views: 2615

Answers (2)

Girish Hosamani
Girish Hosamani

Reputation: 1203

You have to configure sendmail in your system.

If you are using ubuntu follow the steps: If you want to use a Gmail account as a free SMTP server on your Ubuntu-Linux server, you will find this article useful. This guide is tested with Ubuntu 12.04. If you face any issue, feel free to use comments-section below. Relaying Postfix mails via smtp.gmail.com:

First, install all necessary packages:

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

If you do not have postfix installed before, postfix configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com

Then open your postfix config file:

vim /etc/postfix/main.cf

and following lines to it:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

You might have noticed that we haven’t specified our Gmail username and password in above lines. They will go into a different file. Open/Create

vim /etc/postfix/sasl_passwd

And add following line:

[smtp.gmail.com]:587    [email protected]:PASSWORD

If you want to use your Google App’s domain, please replace @gmail.com with your @domain.com

Fix permission and update postfix config to use sasl_passwd file:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Next, validate certificates to avoid running into error. Just run following command:

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Finally, reload postfix config for changes to take effect:

sudo /etc/init.d/postfix reload

Testing Check if mails are sent via Gmail SMTP server

If you have configured everything correctly, following command should generate a test mail from your server to your mailbox.

echo "Test mail from postfix" | mail -s "Test Postfix" [email protected]

For more details : https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

Thanks to rtcamp.com, It helped me a lot :-)

Upvotes: 3

Girish
Girish

Reputation: 12127

Google mail required ssl for SMTP auth so open_ssl should be enable extension in your php.ini

Codeigniter mail library more details about SSL/TLS click here

Enable OPEN SSL in php click here

Upvotes: 0

Related Questions