Codeigniter 4以HTML作为邮件发送的电子邮件

ogsagwnx  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(169)

你能帮我想出一种方法来使用CI4的email类发送一封HTML邮件吗?
在Codeigniter 3中,它很简单,如下所示:

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

但在Codeigniter 4中,我尝试这样做:

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

它给出一个错误:

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

我还尝试将视图放入一个变量中,如下所示:

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

但它只是抛出相同的错误,我在互联网上搜索所有的文档,但无法找到一种方法来做到这一点.请帮助.
我尝试了这个,但没有得到错误,也没有得到电子邮件:'(

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

rur96b6h1#

根据this,如果你想使用一些模板作为你的消息正文,你应该这样做:

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

代码点火器4文档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');
dgsult0t

dgsult0t2#

好的,我现在知道了,我通过添加代码$email-〉setNewLine(“\r\n”)使它工作;在setMessage:

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

并且我还将SMTP端口设置为587而不是465:

$config['SMTPPort']= 587;

同样,对于setMessage,我是这样做的:

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

真的很奇怪的人....

ifsvaxew

ifsvaxew3#

**A.**首先,

代替:
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
使用这个:
$echo_page = view('html_page_to_send_from_view_folder',$data);

  • 请注意echo语句的运气,它没有true作为view(...) heppler函数的第三个参数传递。*
    **B.**其次,要提交基于HTML的电子邮件,请确保已将mailType属性设置为html。这可以通过在Email示例上使用setMailType()方法来实现。例如:

$email->setMailType('html');
或者,您可以通过向email initialize()方法传递一个首选项值数组来设置 “mail type”,即:

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;

}

相关问题