• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending email from gmail

#1
I'm trying to send an email from my gmail account to some other gmail address. But my code doesn't seem to work at all.

PHP Code:
$config = array(
"protocol"  => "smtp",
"smtp_host" => "smtp.gmail.com",
"smtp_user" => "[email protected]",
"smtp_pass" => "xxxxxxxx",
"smtp_port" => 587,
"smtp_crypto"=> "tls"
);

$this->load->library("email");
//$this->load->library('encryption'); 
$this->email->initialize($config);

$this->email->from("[email protected]");
$this->email->to("[email protected]");
$this->email->message("Hello");
$this->email->subject("Test");
$this->email->send(); 

I have allowed less secure apps to access gmail. Also I'm executing this script on local computer. My PHP version is 7.2.11. Codeigniter version is 3.1.10
Reply

#2
You should run the debug tool. It will tell you want's wrong.

https://codeigniter.com/user_guide/libraries/email.html
Reply

#3
Debugger says this.

Code:
220 smtp.gmail.com ESMTP y24sm14013191pge.72 - gsmtp
hello: 250-smtp.gmail.com at your service, [115.69.242.169]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
starttls: 220 2.0.0 Ready to start TLS
hello: 250-smtp.gmail.com at your service, [115.69.242.169]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
from: 250 2.1.0 OK y24sm14013191pge.72 - gsmtp
to: 250 2.1.5 OK y24sm14013191pge.72 - gsmtp
data: 354  Go ahead y24sm14013191pge.72 - gsmtp

quit:
The following SMTP error was encountered:
The following SMTP error was encountered:
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
Date: Sun, 16 Feb 2020 12:15:12 +0100
From: <[email protected]>
Return-Path: <[email protected]>
To: [email protected]
Subject: =?UTF-8?Q?Test?=
Reply-To: <[email protected]>
User-Agent: CodeIgniter
X-Sender: [email protected]
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
Mime-Version: 1.0


Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Hello


My php.ini is like this

Code:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=587

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
Reply

#4
PHP Code:
$config['protocol']  'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'your_email';
$config['smtp_pass'] = 'your_password';
$config['smtp_port'] = 465;
$config['charset']   'utf-8';
$config['mailtype']  'html';
$config['newline']   "\r\n"

Try that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(02-16-2020, 04:37 AM)InsiteFX Wrote:
PHP Code:
$config['protocol']  'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_user'] = 'your_email';
$config['smtp_pass'] = 'your_password';
$config['smtp_port'] = 465;
$config['charset']   'utf-8';
$config['mailtype']  'html';
$config['newline']   "\r\n"

Try that.

That worked!

Actually I had seen that example on stackoverflow. But used to remove the variable $config['newline'] = "\r\n", which seems to be crucial. Also smtp_host must be in the format in example. (I had used smtp.gmail.com, which works in PHPMailer script but not in codeigniter)

Thanks!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.