有没有一种方法可以使用WordPress的wp_mail()函数发送HTML格式的电子邮件?
5 个回答
- 投票数
-
- 2011-09-06
来自 wp_mail抄本页面:
默认内容类型为"文本/纯文本",不允许使用HTML.但是,您可以使用" wp_mail_content_type"过滤器来设置电子邮件的内容类型.
// In theme's functions.php or plug-in code: function wpse27856_set_content_type(){ return "text/html"; } add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
from wp_mail codex page:
The default content type is 'text/plain' which does not allow using HTML. However, you can set the content type of the email by using the 'wp_mail_content_type' filter.
// In theme's functions.php or plug-in code: function wpse27856_set_content_type(){ return "text/html"; } add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
-
嗯,听起来很有用.只是一个问题,为什么要命名函数wpse27856_set_content_type的任何特定原因?Hmm sounds useful. Just a question, any particular reason why you named your function wpse27856_set_content_type?
- 2
- 2011-09-06
- racl101
-
不,它只是基于此特定问题的ID的唯一名称.wpse=wp stachexchange,27856是URL中此问题的ID.我这样做是为了避免人们在此处复制/粘贴代码时可能发生的冲突.No, it's just a unique name based on the id of this particular question. wpse = wp stachexchange, 27856 is the id of this question in the URL. I just do that to avoid potential collisions if people copy/paste code out of here.
- 18
- 2011-09-06
- Milo
-
您也可以只在邮件标题中包含Content-Type.看看Notifly插件是如何做到的.You can also just include the Content-Type in your email headers. Check out how the Notifly plugin does it.
- 3
- 2011-09-07
- Otto
-
哦,是的,哈哈我是n00b.猜猜这是这篇文章的ID.oh yeah, ha ha. What a n00b I am. Guess it is the id of this post.
- 0
- 2011-09-07
- racl101
-
电子邮件应该是.txt文件还是.html文件?我正在使用此方法,但是如果我查看源代码,则它是一个.txt文件,并且嵌入式图像未得到处理.Should the email be a .txt file or a .html file? I'm using this method but if I view source it is a .txt file and the embedded image is not processed.
- 0
- 2012-07-27
- AlxVallejo
-
@AlxVallejo,如果您从文件发送,则可能需要首先以字符串形式读取文件.@AlxVallejo if your sending from file you will probably need to read the file as a string first.
- 0
- 2013-01-28
- Blowsie
-
与添加钩子相比,传递标头是一种更有效的方法.-1passing the headers in is a more efficient method than adding a hook. -1
- 0
- 2016-11-01
- Jeremy
-
@Jeremy当然可以,但是在许多情况下,直接传递标头不是一种选择,例如,当不是您的代码调用`wp_mail`时.@Jeremy sure, but passing the headers directly is not an option in many cases, like when it's not your code calling `wp_mail`.
- 0
- 2016-11-02
- Milo
-
@Milo您是正确的,但对于此问题,标题是正确的答案.@Milo You are correct but for this question the headers are the correct answer.
- 0
- 2016-11-02
- Jeremy
-
这将破坏您的密码重置电子邮件,因为重置链接包含在<>中.This will break your password reset email, because the reset link is wrapped in <>.
- 2
- 2017-10-24
- Simon Josef Kok
-
这不会破坏任何其他期望`wp_mail`发送纯文本电子邮件而不是HTML电子邮件的代码吗?Doesn't this break any other code that expects `wp_mail` to send plain text email, rather than HTML email?
- 0
- 2019-04-04
- Flimm
-
@SimonJosefKok,如果我正确阅读了此错误报告,从WordPress 5.4开始,解决了密码重置电子邮件中断的问题.听起来他们决定从电子邮件地址中删除尖括号.https://core.trac.wordpress.org/ticket/23578#comment:24@SimonJosefKok, if I'm reading this bug report correctly, the issue of breaking password reset emails is resolved as of WordPress 5.4. Sounds like they decided to remove the angle brackets from the email address. https://core.trac.wordpress.org/ticket/23578#comment:24
- 0
- 2020-02-11
- Mark Berry
-
- 2015-07-26
或者,您可以在$ headers参数中指定Content-Type HTTP标头:
$to = '[email protected]'; $subject = 'The subject'; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail( $to, $subject, $body, $headers );
As an alternative, you can specify the Content-Type HTTP header in the $headers parameter:
$to = '[email protected]'; $subject = 'The subject'; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail( $to, $subject, $body, $headers );
-
由于add_filter有时显示为附件,因此效果更好.感谢分享!This works better as the add_filter sometimes shows as attachment. Thanks for sharing!
- 4
- 2018-02-17
- deepakssn
-
这通常是执行此操作的最佳方法.最佳答案将干扰其他插件并引起问题.This is the generally best way to-do this. The top answer will interfere with other plugins and cause problems.
- 2
- 2019-12-06
- Alex Standiford
-
- 2015-01-02
使用wp_mail函数后,请不要忘记删除内容类型过滤器. 按照可接受的答案命名,您应该在执行wp_mail之后执行以下操作:
remove_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
在此处检查此票证-重置内容类型以避免冲突- http://core.trac.wordpress.org/ticket/23578
Don't forget to remove the content type filter after you use the wp_mail function. Following the accepted answer naming you should do this after wp_mail is executed:
remove_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
Check this ticket here - Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
-
这应该是评论,而不是答案,不是吗?This should be a comment, not an answer, no?
- 12
- 2017-07-26
- Bob Diego
-
- 2020-04-05
我将在下面分享另一种简单的方法.甚至您也可以根据需要设置邮件正文的样式.也许对您有帮助.
$email_to = '[email protected]'; $email_subject = 'Email subject'; // <<<EOD it is PHP heredoc syntax $email_body = <<<EOD This is your new <b style="color: red; font-style: italic;">password</b> : {$password} EOD; $headers = ['Content-Type: text/html; charset=UTF-8']; $send_mail = wp_mail( $email_to, $email_subject, $email_body, $headers );
有关PHP heredoc语法的更多信息 https://www.php.net/manual/zh-CN/language.types.string.php#language.types.string.syntax.heredoc
Another easy way I'm going to share below. Even you can style your mail body as your wish. Maybe it's helpful for you.
$email_to = '[email protected]'; $email_subject = 'Email subject'; // <<<EOD it is PHP heredoc syntax $email_body = <<<EOD This is your new <b style="color: red; font-style: italic;">password</b> : {$password} EOD; $headers = ['Content-Type: text/html; charset=UTF-8']; $send_mail = wp_mail( $email_to, $email_subject, $email_body, $headers );
More about PHP heredoc syntax https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
-
- 2020-03-05
使用
ob_start
,因为这将允许您使用WP变量/函数,例如bloginfo等.制作一个PHP文件并将您的HTML粘贴到该文件中(如果需要,请在该php文件中使用wp变量).
使用以下代码:
$to = 'Email Address'; $subject = 'Your Subject'; ob_start(); include(get_stylesheet_directory() . '/email-template.php');//Template File Path $body = ob_get_contents(); ob_end_clean(); $headers = array('Content-Type: text/html; charset=UTF-8','From: Test <[email protected]>'); wp_mail( $to, $subject, $body, $headers );
这将使您的代码保持干净,并且由于ob_start,我们还将节省加载文件的时间.
Use
ob_start
, because this will allow you to use WP variables/functions like bloginfo etc.make a PHP file and paste your HTML in that file(use wp variables inside that php file if needed).
Use the below code:
$to = 'Email Address'; $subject = 'Your Subject'; ob_start(); include(get_stylesheet_directory() . '/email-template.php');//Template File Path $body = ob_get_contents(); ob_end_clean(); $headers = array('Content-Type: text/html; charset=UTF-8','From: Test <[email protected]>'); wp_mail( $to, $subject, $body, $headers );
this will keep your code clean and due to ob_start we will also save the time of loading the file.
是否有一个action_hook或类似的东西可以帮助我实现这一目标?
我尝试在PHP字符串变量中添加标记,并使用
wp_mail()
函数触发了一封电子邮件,如下所示:但是它显示为纯文本吗?
有什么想法吗?