Codeigniter 4 以 HTML 作为消息发送电子邮件
Codeigniter 4 Email sending with HTML as message
你能帮我想办法用CI4的邮箱class发送一封邮件,邮件内容是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);
根据 this,如果你想使用一些模板作为你的消息正文,你应该这样做:
// Using a custom template
$template = view("email-template", []);
$email->setMessage($template);
CodeIgniter 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');
好的,我现在明白了 我通过添加以下代码使其工作 $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);
真是个怪人....
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(...)
的第三个参数传递赫普勒函数。
B. 其次,要提交 HTML-based 电子邮件,请确保您已在 Email 实例上设置 mailType
property to html
. This can be achieved by using the setMailType()
方法。即:
$email->setMailType('html');
或者,您可以通过将首选项值数组传递给电子邮件 initialize()
方法来设置 “邮件类型”。即:
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;
}
你能帮我想办法用CI4的邮箱class发送一封邮件,邮件内容是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);
根据 this,如果你想使用一些模板作为你的消息正文,你应该这样做:
// Using a custom template
$template = view("email-template", []);
$email->setMessage($template);
CodeIgniter 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');
好的,我现在明白了 我通过添加以下代码使其工作 $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);
真是个怪人....
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(...)
的第三个参数传递赫普勒函数。
B. 其次,要提交 HTML-based 电子邮件,请确保您已在 Email 实例上设置 mailType
property to html
. This can be achieved by using the setMailType()
方法。即:
$email->setMailType('html');
或者,您可以通过将首选项值数组传递给电子邮件 initialize()
方法来设置 “邮件类型”。即:
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;
}