CodeIgniter Forums
Cant send Email over SMTP - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Cant send Email over SMTP (/showthread.php?tid=80612)



Cant send Email over SMTP - qtd - 11-21-2021

Hello guys, sorry in advance but i've spent the last two hours trying to send mail with Smtp gmail and it's driving me crazy.
I've tried many things but nothing worked.
class Email extends BaseConfig

Here is a part of my conf file:
{
    /**
    * @var string
    */
    public $fromEmail;

    /**
    * @var string
    */
    public $fromName;

    /**
    * @var string
    */
    public $recipients;

    /**
    * The "user agent"
    *
    * @var string
    */
    public $userAgent = 'CodeIgniter';

    /**
    * The mail sending protocol: mail, sendmail, smtp
    *
    * @var string
    */
    public $protocol = 'smtp';

    /**
    * The server path to Sendmail.
    *
    * @var string
    */
    public $mailPath = '/usr/sbin/sendmail';

    /**
    * SMTP Server Address
    *
    * @var string
    */
    public $SMTPHost = 'smtp.gmail.com';

    /**
    * SMTP Username
    *
    * @var string
    */
    public $SMTPUser= '[email protected]';

    /**
    * SMTP Password
    *
    * @var string
    */
    public $SMTPPass = 'myAppPassword';

    /**
    * SMTP Port
    *
    * @var int
    */
    public $SMTPPort = 465;

    /**
    * SMTP Timeout (in seconds)
    *
    * @var int
    */
    public $SMTPTimeout = 20;

    /**
    * Enable persistent SMTP connections
    *
    * @var bool
    */
    public $SMTPKeepAlive = false;

    /**
    * SMTP Encryption. Either tls or ssl
    *
    * @var string
    */
    public $SMTPCrypto = 'ssl';

    /**
    * Enable word-wrap
    *
    * @var bool
    */
    public $wordWrap = true;

    /**
    * Character count to wrap at
    *
    * @var int
    */
    public $wrapChars = 76;

    /**
    * Type of mail, either 'text' or 'html'
    *
    * @var string
    */
    public $mailType = 'html';

    /**
    * Character set (utf-8, iso-8859-1, etc.)
    *
    * @var string
    */
    public $charset = 'UTF-8';

    /**
    * Whether to validate the email address
    *
    * @var bool
    */
    public $validate = false;

    /**
    * Email Priority. 1 = highest. 5 = lowest. 3 = normal
    *
    * @var int
    */
    public $priority = 3;

    /**
    * Newline character. (Use “\r\n” to comply with RFC 822)
    *
    * @var string
    */
    public $CRLF = "\rn";

    /**
    * Newline character. (Use “\r\n” to comply with RFC 822)
    *
    * @var string
    */
    public $newline = "\r\n";
}


Here is the controller method i'm using:


class SendMail extends Controller
{

    public function index()
{
        return view('form_view');
    }

    function sendMail() {
        $to = $this->request->getVar('mailTo');
        $subject = $this->request->getVar('subject');
        $message = $this->request->getVar('message');
       
        $email = \Config\Services::email();

        $email->setTo($to);
        $email->setFrom('[email protected]', 'Confirm Registration');
       
        $email->setSubject($subject);
        $email->setMessage($message);

        if ($email->send())
{
            echo 'Email successfully sent';
        }
else
{
            $data = $email->printDebugger;
            print_r($data);
        }
    }

}
And i have this error:

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

I've tried my smtp credentials and i have this result:
Code:
Connected to smtp://smtp.googlemail.com:587/?starttls=always
<< 220 smtp.googlemail.com ESMTP q10sm10630086pjd.0 - gsmtp
>> EHLO [172.31.10.74]
<< 250-smtp.googlemail.com at your service, [35.161.55.56]
<< 250-SIZE 35882577
<< 250-8BITMIME
<< 250-STARTTLS
<< 250-ENHANCEDSTATUSCODES
<< 250-PIPELINING
<< 250-CHUNKING
<< 250 SMTPUTF8
>> STARTTLS
<< 220 2.0.0 Ready to start TLS
>> EHLO [172.31.10.74]
<< 250-smtp.googlemail.com at your service, [35.161.55.56]
<< 250-SIZE 35882577
<< 250-8BITMIME
<< 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
<< 250-ENHANCEDSTATUSCODES
<< 250-PIPELINING
<< 250-CHUNKING
<< 250 SMTPUTF8
>> AUTH PLAIN AGJhbG9oZTM3QGdtYWlsLmNvbQBqaGtjZnlzdGd1bnhkcHpm
<< 235 2.7.0 Accepted
>> MAIL FROM:<[email protected]> SIZE=562
>> RCPT TO:<[email protected]>
<< 250 2.1.0 OK q10sm10630086pjd.0 - gsmtp
<< 250 2.1.5 OK q10sm10630086pjd.0 - gsmtp
>> DATA
<< 354 Go ahead q10sm10630086pjd.0 - gsmtp
>> From: [email protected]
>> Date: Sun, 21 Nov 2021 13:34:02 퍍
>> Subject: SMTP test from smtp.googlemail.com
>> Message-Id: <BATC6RLRCFU4.4XILXP0D9O9M1@WIN-AUIR3RRGP88>
>> To: [email protected]
>> MIME-Version: 1.0
>> Content-Type: multipart/alternative; boundary="=-YpqV2WjyFrcACtN榚岊=="
>>
>> --=-YpqV2WjyFrcACtN榚岊==
>> Content-Type: text/plain; charset=utf-8
>>
>> Test message
>> --=-YpqV2WjyFrcACtN榚岊==
>> Content-Type: text/html; charset=utf-8
>> Content-Id: <BATC6RLRCFU4.IA0J48NKH0TT3@WIN-AUIR3RRGP88>
>>
>> <b>Test message</b>
>> --=-YpqV2WjyFrcACtN榚岊==--
>> .
<< 250 2.0.0 OK 1637501643 q10sm10630086pjd.0 - gsmtp


I know there are several culprits:
Use double quotes around \r\n
I have a 2 step verification in gmail so i added the app and put the password in the conf

I use win 10 and laragon with php 7.4.1
My store is in dev mode and i use the last version of CI4
I'm pretty sure that the problem is easily fixable but it is driving me crazy
Thanks in advance


RE: Cant send Email over SMTP - chakycool - 11-24-2021

try adding these 2 variable on Email.php as well

PHP Code:
    public $fromEmail '[email protected]';

    public 
$fromName 'Some-name'



RE: Cant send Email over SMTP - captain-sensible - 11-24-2021

if you don't get any joy then you may look at PHPMailer i use it with google 2 step on a web and it works no problem


RE: Cant send Email over SMTP - InsiteFX - 11-25-2021

Here is an example for google mail.

Codeigniter 4 Send Email with SMTP Tutorial with Example


RE: Cant send Email over SMTP - mlurie - 11-30-2021

This is probably not your issue, but for others who may stumble upon this thread, make sure your hosting provider allows outbound e-mail to go through an external SMTP server. Many hosts (including mine) are now blocking outbound connections over ports 465 and 587 to prevent spammer abuse and forcing all outbound e-mail to go through their local SMTP servers.


RE: Cant send Email over SMTP - InsiteFX - 12-01-2021

Here is another example of how to do it.

How to Send Email in CodeIgniter 4 Using Gmail SMTP