我有几种发送邮件的表格。一些邮件应以html格式发送,其他邮件应以纯文本格式发送。现在,我将html选项设置为:
add_action( \'phpmailer_init\', \'mailer_config\', 10, 1);
function mailer_config(PHPMailer $mailer){
$mailer->IsHTML(true);
}
但这意味着所有邮件都是以html格式发送的。如何在每个表单/邮件的基础上更改此行为?我有几种发送邮件的表格。一些邮件应以html格式发送,其他邮件应以纯文本格式发送。现在,我将html选项设置为:
add_action( \'phpmailer_init\', \'mailer_config\', 10, 1);
function mailer_config(PHPMailer $mailer){
$mailer->IsHTML(true);
}
但这意味着所有邮件都是以html格式发送的。如何在每个表单/邮件的基础上更改此行为?好的,根据@birgire的建议,我最终使用wp_mail_content_type
与我的表单上的隐藏字段一起过滤。Php代码如下:
add_filter( \'wp_mail_content_type\', \'set_mailer_content_type\' );
function set_mailer_content_type( $content_type ) {
if(isset($_POST[\'ishtmlform\'])){ return \'text/html\'; } // in-page/form hidden field
return \'text/plain\';
}
这使我可以在每个页面上有多个表单,并具有不同的内容类型设置(例如,一个发送文本/纯文本,另一个发送文本/html,就在同一html页面中)。旁注,有点离题:
内部,wp_mail()
刚刚设置PhpMailer->isHTML(true)
如果设置内容类型==\'text/html\'
. 您可以在中找到源wp-includes/pluggable.php
\'text/html\', 您必须发送真正的html代码,我的意思是,邮件正文必须至少包含html、标题、标题和正文标记,而不仅仅是一些带有br或链接的纯文本,否则它可能会被标记为“非纯文本”,您的邮件会被识别为垃圾邮件
这里有一个(未测试的)PHPMailer示例,可以检查主题和内容类型:
function mailer_config( PHPMailer $mailer ) {
if( \'Subject #1\' === $mailer->Subject && \'text/html\' !== $mailer->ContentType ) {
$mailer->IsHTML( true );
}
}
其他选项包括检查$mailer->From
或$mailer->FromName
, 或其他一些条件,具体取决于您的设置。没有PHPMailer依赖关系的另一种方法是使用wp_mail
过滤器,带有wp_mail_content_type
滤器
重力形式。我尝试在渲染表单字段之前对其进行操作add_filter(\"gform_pre_render\", \"my_function\", 10, 5); function my_function($form){ ... $form[\"fields\"][0][\"content\"] = \'This is a html-block\' } 这样,我可以传递html块的内容,假设html是表单上的第一个字段。如何通过id? 假设上面的html块