Welcome Guest, Not a member yet? Register   Sign In
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" => "xxxx@xxxx",
"smtp_pass" => "xxxxxxxx",
"smtp_port" => 587,
"smtp_crypto"=> "tls"
);

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

$this->email->from("xxxx@xxxx");
$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

(This post was last modified: 02-16-2020, 06:30 AM by hmvartak.)

(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
#6

(This post was last modified: 04-05-2021, 01:56 AM by Corsari.)

For those that are searching an updated tutorial

Published march the 31 in 2021

check out this one
https://www.positronx.io/codeigniter-sen...h-example/

WARNING: for those of you who can't succeed in the SMTP authentication


since I've lost some time on this issue

For the sake of avoid somebody else to loose time

If you can't authenticate, read carefully the messages with the debug options

I have been deceived by these authentication errors

end it ended up with being the connection ports 465 and 587 being not accessible from my computer to remote SMTP server

e.g. xampp in windows

well, the issue was Avast antivirus and with modern antiviruses the issue could happen either with third party antiviruses

So in the case you can't send emails, a quick troubleshoot is to disable antivirus and make a test
Reply




Theme © iAndrew 2016 - Forum software by © MyBB