Search code examples
phpcodeignitercodeigniter-4

Codeigniter 4 Email sending with HTML as message


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);

Solution

  • 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....