Home > Blockchain >  Codeigniter 4 Email sending with HTML as message
Codeigniter 4 Email sending with HTML as message

Time:06-02

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

CodePudding user response:

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

CodePudding user response:

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

  • Related