Contact Form 7 - 获取最终邮件 HTML 输出
Contact Form 7 - Get final mail HTML output
是否有一个钩子可以让我准确地看到发送到电子邮件的内容?
我试过使用 'wpcf7_mail_sent'
,它只包含数据和字段的数组。
例如它有 "first-name": "John", "last-name": "Smith", ...
等但没有模板。
我想要得到的是与该数据合并的邮件模板...也就是最终电子邮件 HTML:
例如:Hello, John Smith! <br> Thanks for contacting us.
编辑:澄清
(使用@Howard E提供的例子)
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission', 10, 3 );
function dd_handle_form_submission( $contact_form,$abort,$submission ) {
$template = $contact_form->prop('mail')['body'];
$posted_data = $submission->get_posted_data();
//Outputs:
//$template: <h2> First Name: [first-name] </h2> <h2> Last Name: [last-name] </h2> ...
//$posted_data: Array ( [first-name] => 'John', [last-name] => 'Smith' ... )
}
那是模板,但带有短代码...和数据,但作为数组。
我最初的问题是如何获得合并后的 $posted_data
+ $template
的最终输出,所以 $html
var 看起来像这样:
"<h2> First Name: John </h2> <h2> Last Name: Smith </h2>"
...
出于安全原因,contact form 7 不允许 html 但使用下面的挂钩您可以获取表单数据并以 html 格式发送电子邮件。
// define the wpcf7_before_send_mail callback
function action_wpcf7_before_send_mail( $contact_form ) {
// Use wp_mail() to send an email. To send html in body first add_filter with content type text/html within this function
add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
//Send email..... Even you can put php variable values within html
wp_mail( 'me@example.net', 'your_subject', '<div>The message</div>' );
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
电子邮件正文在 class WPCF7_ContactForm
中传递给挂钩 wpcf7_before_send_mail
使用方法 prop
从那里访问 mail
。
此时,您可以挂钩邮件并根据需要更新输出。使用 $contact_form->set_properties(array('mail' => $mail));
将邮件正文更新为您想要的任何内容。不需要 return 函数,因为您正在直接更新对象。
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission' );
function dd_handle_form_submission( $contact_form ) {
$mail = $contact_form->prop('mail')['body'];
// Output the content to the error log of your website.
ob_start();
echo $mail;
error_log(ob_get_clean());
// Use this to push new content to the mail before sending
$mail = $new_mail_content // whatever you set it to
$contact_form->set_properties(array('mail' => $mail));
}
更新答案:
要在变量替换后获取电子邮件的内容,您必须使用过滤器 wpcf7_mail_components
$components
是邮件组件的数组
['subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments']
由于这个函数没有输出到屏幕,你必须将它发送到错误日志来调试它。
add_filter('wpcf7_mail_components', 'filter_mail_components', 10, 3);
function filter_mail_components($components, $current_form, $mail_class){
ob_start();
print_r($components['body']);
error_log(ob_get_clean());
return $components;
}
是否有一个钩子可以让我准确地看到发送到电子邮件的内容?
我试过使用 'wpcf7_mail_sent'
,它只包含数据和字段的数组。
例如它有 "first-name": "John", "last-name": "Smith", ...
等但没有模板。
我想要得到的是与该数据合并的邮件模板...也就是最终电子邮件 HTML:
例如:Hello, John Smith! <br> Thanks for contacting us.
编辑:澄清
(使用@Howard E提供的例子)
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission', 10, 3 );
function dd_handle_form_submission( $contact_form,$abort,$submission ) {
$template = $contact_form->prop('mail')['body'];
$posted_data = $submission->get_posted_data();
//Outputs:
//$template: <h2> First Name: [first-name] </h2> <h2> Last Name: [last-name] </h2> ...
//$posted_data: Array ( [first-name] => 'John', [last-name] => 'Smith' ... )
}
那是模板,但带有短代码...和数据,但作为数组。
我最初的问题是如何获得合并后的 $posted_data
+ $template
的最终输出,所以 $html
var 看起来像这样:
"<h2> First Name: John </h2> <h2> Last Name: Smith </h2>"
...
出于安全原因,contact form 7 不允许 html 但使用下面的挂钩您可以获取表单数据并以 html 格式发送电子邮件。
// define the wpcf7_before_send_mail callback
function action_wpcf7_before_send_mail( $contact_form ) {
// Use wp_mail() to send an email. To send html in body first add_filter with content type text/html within this function
add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
//Send email..... Even you can put php variable values within html
wp_mail( 'me@example.net', 'your_subject', '<div>The message</div>' );
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
电子邮件正文在 class WPCF7_ContactForm
中传递给挂钩 wpcf7_before_send_mail
使用方法 prop
从那里访问 mail
。
此时,您可以挂钩邮件并根据需要更新输出。使用 $contact_form->set_properties(array('mail' => $mail));
将邮件正文更新为您想要的任何内容。不需要 return 函数,因为您正在直接更新对象。
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission' );
function dd_handle_form_submission( $contact_form ) {
$mail = $contact_form->prop('mail')['body'];
// Output the content to the error log of your website.
ob_start();
echo $mail;
error_log(ob_get_clean());
// Use this to push new content to the mail before sending
$mail = $new_mail_content // whatever you set it to
$contact_form->set_properties(array('mail' => $mail));
}
更新答案:
要在变量替换后获取电子邮件的内容,您必须使用过滤器 wpcf7_mail_components
$components
是邮件组件的数组
['subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments']
由于这个函数没有输出到屏幕,你必须将它发送到错误日志来调试它。
add_filter('wpcf7_mail_components', 'filter_mail_components', 10, 3);
function filter_mail_components($components, $current_form, $mail_class){
ob_start();
print_r($components['body']);
error_log(ob_get_clean());
return $components;
}