Sending Email using gmail account

edited March 2022 in PHP

Before using gmail account to send email, few changes needed to be done to the gmail account.

  1. To allow account to login on less secure apps. You can find this in settings of your gmail account. You can go through this link and find more information https://support.google.com/mail/answer/6562?hl=en&co=GENIE.Platform%3DDesktop

After that was done, the code to send the mail can be attached here..

function send_email($to_mail, $subject, $body) {

//print_r($to_mail);exit;

$config = Array(

'protocol' => 'smtp',

'smtp_host' => 'smtp.gmail.com',

'smtp_port' => '587',

'smtp_user' => 'gmail_id',

'smtp_pass' => 'gmail_password',

'mailtype' => 'html',

'charset' => 'utf-8',

'wordwrap' => TRUE

);

$config['smtp_crypto'] = 'tls';

$config['newline'] = "\r\n";

$this->load->library('email', $config);

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

$from_mail ='gmail_id';

$this->email->from($from_mail,'HSL App');

$this->email->to($to_mail);

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

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

$a =$this->email->send();

if($a) {

return 1;

}else{

print_r($this->email->print_debugger(array('headers')));

return 0;

}

}

Note: The variables to_mail, subject, body are passed as parameters or arguments from another function.

Sign In or Register to comment.