CodeIgniter Forums

Full Version: Sending email using smtp doesn't work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following code doesn't work (no email is received) 
PHP Code:
<?php namespace App\Controllers;

class 
Email extends BaseController
{
    public function 
index()
    {
        $config['protocol'] = 'smtp';
        $config['charset']  'iso-8859-1';
        $config['wordWrap'] = true;
        $config['SMTPHost'] = 'ssl://smtp.gmail.com';
        $config['SMTPUser'] = '[email protected]';
        $config['SMTPPass'] = 'xxx';
        $config['SMTPCrypto'] = 'ssl'// tls or ssl
        $config['SMTPPort'] = 465// tls:587  ssl:465
        $config['mailType'] = 'html';

        $email = \Config\Services::email();
        $email->initialize($config); // Use the config above

        $email->setFrom('[email protected]''xxx');
        $email->setTo('[email protected]');
        $email->setSubject('Email Test');
        $email->setMessage('Testing the email class.');
        $email->send();
    }

I have also tried sendgrid without success:
PHP Code:
$config['protocol']         'smtp';
        $config['smtpHost']        'smtp.sendgrid.net';
        $config['smtpPort']        587;
        $config['smtpUser']        'apikey';
        $config['smtpPass']        'my_key';
        $config['charset']          'utf-8';
        $config['mailType']         'html';
        $config['SMTPCrypto'] = 'tls'// tls or ssl

       

        $email 
= \Config\Services::email();
        $email->initialize($config); // Use the config above

        $email->setFrom('[email protected]''xxx');
        $email->setTo('[email protected]');
        $email->setSubject('Email Test');
        $email->setMessage('Testing the email class.');
        $email->send(); 
Works fine with:

public $fromEmail = '[email protected]';
public $protocol = 'smtp';
public $SMTPHost = 'smtp.gmail.com';
public $SMTPUser = '[email protected]';
public $SMTPPass = '********';
public $SMTPPort = 587;
public $SMTPCrypto = 'tls';
(04-14-2020, 06:27 AM)elephpantech Wrote: [ -> ]Works fine with:

public $fromEmail = '[email protected]';
public $protocol = 'smtp';
public $SMTPHost = 'smtp.gmail.com';
public $SMTPUser = '[email protected]';
public $SMTPPass = '********';
public $SMTPPort = 587;
public $SMTPCrypto = 'tls';
Thanks, that works. Is was wrong in the setting of port/crypto and host. I will now start to test using Sendgrid or another email service.