以编程方式在联系表单 7 中发送自定义消息之前,我无法在电子邮件正文中发送自定义消息。
这是我在functions.php中编写的代码
add_action( 'wpcf7_mail', 'my_custom_contact_form_7_mail_function', 10, 2 );
function my_custom_contact_form_7_mail_function( $contact_form, $email ) {
// Get the contact form fields.
$contact_form_fields = $contact_form->get_posted_data();
// Create a custom message.
$custom_message = 'This is my custom message.';
$custom_message .= ' ' . $field_name . ': ' . $field_value;
// Append the custom message to the email body.
$email['body'] = str_replace("[custom-message]", "$custom_message");
}
这是我的联系表 7 邮件设置:
wpcf7_mail
甚至看起来都不是你可以挂钩的东西。您可能想在 filter
上使用 action
而不是 wpcf7_mail_components
。
您需要
$contact_form_fields
做什么?没用过!
您的函数使用
$field_name
和 $field_value
,但您没有定义这些变量。
str_replace
接受三个或四个参数,而不仅仅是两个。是search
、replace
、subject
。
函数的参数不是
by reference
,因此您需要返回新值。
这应该有效:
function my_wpcf7_mail_components($components, $form, $mail_object) {
$submission = WPCF7_Submission::get_instance();
// Get the contact form fields.
$contact_form_fields = $submission->get_posted_data();
// Create a custom message.
$custom_message = 'This is my custom message.';
$custom_message .= ' ' . $contact_form_fields['your-name'] . ' has sent you a message.';
// Append the custom message to the email body.
$components['body'] = str_replace("[custom-message]", $custom_message, $components['body']);
return $components;
}
add_filter( 'wpcf7_mail_components', 'my_wpcf7_mail_components', 10, 3 );