2

can you help me figure out a way to use the email class of CI4 to send an email with an HTML as the message?

in Codeigniter 3 it is simple and goes like this:

$email->message($this->load->view('html_page_to_send_from_view_folder',$data,true));

but in Codeigniter 4 I tried doing this:

$email->setMessage(echo view('html_page_to_send_from_view_folder',$data,true));

It gives out an error:

syntax error, unexpected echo(T_ECHO), expecting ')'

I also tried putting the view in a variable like so:

$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);

but it just throws the same error, I was searching all over the internet and the documentation but couldn't find a way to do this. Help please.

I tried this but got no error, and also got no email :'(

$echo_page = view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);

4 Answers4

1

According to this, if you want to use some template as your message body, you should do something like this:

// Using a custom template
$template = view("email-template", []);
    
$email->setMessage($template);

CodeIgniter 4 documentation states:

setMessage($body)
Parameters:  $body (string) – E-mail message body
Returns:     CodeIgniter\Email\Email instance (method chaining)
Return type: CodeIgniter\Email\Email

Sets the e-mail message body:
$email->setMessage('This is my message');
0

Okay I got it now I made it work by adding this code $email->setNewLine("\r\n"); at the end just after the setMessage:

$email->setMessage($my_message);    
$email->setNewLine("\r\n");

and also I set the SMTP port 587 instead of 465:

$config['SMTPPort']= 587;

ALSO, for the setMessage, I did it like this:

$my_message = view('html_page_to_send_from_view_folder',["id" => $data['id']]);
$email->setMessage($my_message);

really weird man....

0

A. Firstly,

Instead of:❌

$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);

Use this:✅

$echo_page = view('html_page_to_send_from_view_folder',$data);

Notice the luck of an echo statement and not passing true as the third argument of the view(...) hepler function.

B. Secondly, to submit HTML-based emails, ensure that you've set the mailType property to html. This can be achieved by using the setMailType() method on the Email instance. I.e:

$email->setMailType('html');

Alternatively, you could set the "mail type" by passing an array of preference values to the email initialize() method. I.e:

public function sendMail(): bool
{
    $email = \Config\Services::email();

    $email->initialize([
        'SMTPHost' => 'smtp.mailtrap.io',
        'SMTPPort' => 2525,
        'SMTPUser' => '479d7c109ae335',
        'SMTPPass' => '0u6f9d18ef3256',
        'SMTPCrypto' => 'tls',
        'protocol' => 'smtp',
        'mailType' => 'html',
        'mailPath' => '/usr/sbin/sendmail',
        'SMTPAuth' => true,
        'fromEmail' => 'from@example.com',
        'fromName' => 'DEMO Company Name',
        'subject' => 'First Email Test',
    ]);

    $email->setTo('to@example.com');
    $email->setMessage(view('blog_view'));

    $response = $email->send();

    $response ? log_message("error", "Email has been sent") : log_message("error", $email->printDebugger());

    return $response;

}
steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
0

Here is a snippet from the CodeIgniter 4 docs that I have modified to send HTML Emails.

You can refer to it from the CI4 documentation in this link -https://codeigniter.com/user_guide/libraries/email.html

<?php

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

$email->setFrom('your@example.com', 'Your Name');
$email->setTo('someone@example.com');
$email->setCC('another@another-example.com');
$email->setBCC('them@their-example.com');

$email->setSubject('Email Test');

// Note: Do not use echo as we need the rendered page as plain text/html
$message = View('path_to_your_email_template_file', ['data' => 'to_your_file']); 

$email->setMessage($message);

// If you forget the line below, it may render the email as plain text(the raw code)

$this->email->setMailType('html');  

$email->send();

You will also need to make changes to you app/config/Email.php file to allow CI4 to send emails from your server.

Kelvin Mboto
  • 361
  • 6
  • 15