How to set up Class email in Codeigniter 4

Asked

Viewed 460 times

-1

I followed several tutorials that I looked for, but I couldn’t work, I don’t know what I’m missing, I’m starting now to mess with Codeigniter and I got version 4. I’m wanting to when fill the form send to email when Submit. But when sending always appears the error

500 (Internal Server Error)

Which is how I’m calling my form controller’s email class.

I’ve already looked at these two sites https://codeigniter.com/user_guide/libraries/email.html#sending-email https://www.tutsmake.com/send-email-in-codeigniter-4-with-smtp/ I understand you have to edit Config/Email.php, but in the controller when called you use

$email = \Config\Services::email();

I don’t know if you have to configure something in Config/Services or another file to move.

This is the Config/Email.php file

    <?php
namespace Config;

use CodeIgniter\Config\BaseConfig;

class Email extends BaseConfig
{

    /**
     * @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 = 'mail';

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

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

    /**
     * SMTP Username
     *
     * @var string
     */
    public $SMTPUser = 'RRRRRRRRRRRR';

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

    /**
     * SMTP Port
     *
     * @var integer
     */
    public $SMTPPort = 587;

    /**
     * SMTP Timeout (in seconds)
     *
     * @var integer
     */
    public $SMTPTimeout = 15;

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

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

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

    /**
     * Character count to wrap at
     *
     * @var integer
     */
    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 boolean
     */
    public $validate = false;

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

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

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

    /**
     * Enable BCC Batch Mode.
     *
     * @var boolean
     */
    public $BCCBatchMode = false;

    /**
     * Number of emails in each BCC batch
     *
     * @var integer
     */
    public $BCCBatchSize = 200;

    /**
     * Enable notify message from server
     *
     * @var boolean
     */
    public $DSN = false;


}

And in my controller of the page that is the form I am calling so


    <?php namespace App\Controllers;
use CodeIgniter\Controller;

class Contato extends BaseController
{
    public function index()
    {
        if (!empty($_POST)) {
            $this->load->library('PHPMailer_Lib');
            $config['protocol'] = 'sendmail';
            $config['mailPath'] = '/usr/sbin/sendmail';
            $config['charset']  = 'iso-8859-1';
            $config['wordWrap'] = true;

            $email->initialize($config);
        }


        $date['isMap'] = true;
        $this->templateNoHome('contato', $date);
    }

    

    //--------------------------------------------------------------------

}

And in Controllerbase I’m calling the email helper

    protected $helpers = ['url', 'form', 'email'];

I don’t know if any more files are missing to call, who can help, I really appreciate.

1 answer

0

I managed to make it work, and by the things I did what was missing and called the email class

use CodeIgniter\Email\Email;

With this worked, follows a method that receives the data of a form and sends by email

    <?php namespace App\Controllers;
use CodeIgniter\Controller;

use CodeIgniter\Email\Email;
use CodeIgniter\HTTP\RequestInterface;

class Sucesso extends Controller
{
    public function index()
    {
    $request = \Config\Services::request();
    if($request->getPost()){

      $nome       = $request->getVar('nome');
      $telefone   = $request->getVar('telefone');
      $emailform  = $request->getVar('email');
      $mensagem   = $request->getVar('mensagem');

      $email = \Config\Services::email();

      $config['mailType']       = 'html';

      $email->initialize($config);
      $email->setFrom('[email protected]');
      $email->setTo('[email protected]');

      $email->setSubject('Formulário de contato');
      $email->setMessage("<!DOCTYPE html>
                      <html lang='en' dir='ltr'>
                        <head>
                          <meta charset='utf-8'>
                          <title></title>
                        </head>
                        <body>
                        <strong>" . $nome . "</strong><br/>" .
                         "<strong>Telefone:</strong> " . $telefone . "<br/>" .
                         "<strong>E-mail:</strong> " . $emailform . "<br/>" .
                         "<strong>Mensagem:</strong> <br/>" . $mensagem .
                      "</body>
                    </html>");
        $email->setAltMessage("Nome: ".$nome."\n\Telefone: ".$telefone."\n\E-mail: ".$emailform."\n\Mensagem: ".$mensagem);

        $email->send();
        //$email->printDebugger();
    }

        echo view('template/header', $data);
        echo view('sucesso', $data);
        echo view('template/footer', $data);
        echo view('template/scripts', $data);
    }


}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.