我目前正在尝试通过 WordPress functions.php 文件将 CC 电子邮件地址动态添加到 Elementor 表单的电子邮件中。我的客户希望我仅在选择下拉/选择字段中的特定值时添加抄送电子邮件地址。我已经设法让它工作,但现在它将这些抄送电子邮件地址添加到两封电子邮件中。 (参考:在提交 Elementor 表单时,通过 WordPress function.php 文件动态添加 CC 或 BCC 电子邮件地址)
我通过以下方式使用提交电子邮件后的 Elementor 表单操作:
这是代码:
##############################################################################
// Action is called when the forms is submitted and being processed
##############################################################################
add_action( 'elementor_pro/forms/process', 'add_cc_emails_to_submitted_elementor_forms', 10, 2 );
function add_cc_emails_to_submitted_elementor_forms( $record, $handler ) {
// Get the form name
$form_name = $record->get_form_settings( 'form_name' );
// Check which form is being processed
if ( $form_name !== 'FORM NAME') {
return;
}
// Create an array of all the fields used in the form
$raw_fields = $record->get( 'fields' );
// Get the selected drop down value
$drop_down_field_value = $raw_fields['field_name']['value'];
// Set the CC email addresses
if( $drop_down_field_value === 'SOMETHING' ){
// Call the filter to apply the needed CC Emails
add_filter( 'elementor_pro/forms/wp_mail_headers', 'add_cc_emails_to_elementor_form', 10, 1 );
}
}
##############################################################################
// Add CC email addresses to the Email Header
##############################################################################
function add_cc_emails_to_elementor_form( $headers ) {
// Set all the CC email addresses
$headers .= "Cc: [email protected]\r\n";
$headers .= "Cc: [email protected]\r\n";
return $headers;
}
有没有办法让我根据正在发送的电子邮件设置标题。我只需要将抄送电子邮件地址添加到电子邮件 1(发送给网站所有者的电子邮件),而不是电子邮件 2。开发人员文档上的信息 (https://developers.elementor.com/docs/hooks/forms /)不要提及任何事情。
这是实现此目的的代码的修改版本:
add_action( 'elementor_pro/forms/process', 'add_cc_emails_to_submitted_elementor_forms', 10, 2 );
function add_cc_emails_to_submitted_elementor_forms( $record, $handler ) {
// Get the form name
$form_name = $record->get_form_settings( 'form_name' );
// Check which form is being processed
if ( $form_name !== 'FORM NAME') {
return;
}
// Create an array of all the fields used in the form
$raw_fields = $record->get( 'fields' );
// Get the selected drop down value
$drop_down_field_value = $raw_fields['field_name']['value'];
// Set the CC email addresses only for Email 1
if( $drop_down_field_value === 'SOMETHING' ){
// Call the filter to apply the needed CC Emails
add_filter( 'elementor_pro/forms/wp_mail_headers', 'add_cc_emails_to_elementor_form', 10, 2 );
}
}
// Add CC email addresses to the Email Header only for Email 1
function add_cc_emails_to_elementor_form( $headers, $record ) {
// Get the email type
$email_type = $record->get_form_settings( 'email_type' );
// Check if the email type is Email 1
if ( 'email' === $email_type ) {
// Set all the CC email addresses
$headers .= "Cc: [email protected]\r\n";
$headers .= "Cc: [email protected]\r\n";
}
return $headers;
}
在此修改后的代码中:
- 在添加抄送电子邮件地址之前,我们添加了一项检查以验证电子邮件类型是否为电子邮件 1。
- 这可确保抄送电子邮件地址仅添加到电子邮件 1,而不添加到电子邮件 2。