简体   繁体   English

Codeigniter 4 使用 HTML 作为消息发送电子邮件

[英]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?你能帮我找出一种方法来使用 CI4 的电子邮件类发送带有 HTML 作为消息的电子邮件吗?

in Codeigniter 3 it is simple and goes like this:在 Codeigniter 3 中,它很简单,如下所示:

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

but in Codeigniter 4 I tried doing this:但在 Codeigniter 4 中,我尝试这样做:

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

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 : CodeIgniter 4 文档状态

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

Okay I got it now I made it work by adding this code $email->setNewLine("\r\n");好的,我现在知道了,我通过添加此代码使其工作 $email->setNewLine("\r\n"); at the end just after the setMessage:在 setMessage 之后的最后:

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

and also I set the SMTP port 587 instead of 465:我还设置了 SMTP 端口 587 而不是 465:

$config['SMTPPort']= 587;

ALSO, for the setMessage, I did it like this:另外,对于 setMessage,我是这样做的:

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

really weird man....真是个奇怪的人……

A. Firstly, A.首先,

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.请注意echo语句的运气,并且没有true作为view(...) hepler 函数的第三个参数传递。

B. Secondly, to submit HTML-based emails, ensure that you've set the mailType property to html . B.其次,要提交基于 HTML 的电子邮件,请确保您已将mailType属性设置为html This can be achieved by using the setMailType() method on the Email instance.这可以通过在电子邮件实例上使用setMailType()方法来实现。 Ie: IE:

$email->setMailType('html');

Alternatively, you could set the "mail type" by passing an array of preference values to the email initialize() method.或者,您可以通过将一组首选项值传递给电子邮件initialize()方法来设置“邮件类型” Ie: IE:

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM