基于联系表 7 的邮政编码输入的动态电子邮件收件人
Dynamic Email Recipient based on ZIP Code Input for Contact Form 7
我们有 4 个办事处,分配了很多邮政编码:
- 21079, 22085,.... = 汉堡
- 10115, 10178,.... = 柏林
- 60306, 60312,.... = 法兰克福
- .......,......,.... = 慕尼黑
- 其他所有 = 总部
是否可以从表单中读出输入的邮政编码并将邮件动态发送到指定的办公室?
它必须是动态的,没有所有邮政编码或城市的下拉列表(在前端)。
我试过了没有成功
// hook into wpcf7_before_send_mail
add_action( 'wpcf7_before_send_mail', 'cf7dynamicnotifications'); // Hooking into wpcf7_before_send_mail
function cf7dynamicnotifications($contact_form) // Create our function to be used in the above hook
{
$submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class
$posted_data = $submission->get_posted_data(); // Get all of the submitted form data
if( $posted_data["plz"] == '21079' ) {
$recipient_email = 'office1@xyz.com';
}
elseif($posted_data["plz"] == '22085') {
$recipient_email = 'office2@xyz.com';
}
elseif($posted_data["plz"] == '12345') {
$recipient_email = 'office3@xyz.com';
}
else {
$recipient_email = 'head-office@xyz.com';
}
// set the email address to recipient
$mailProp = $contact_form->get_properties('mail');
$mailProp['mail']['recipient'] = $recipient_email;
// update the form properties
$contact_form->set_properties(array('mail' => $mailProp['mail']));
}
编辑
感谢您的帮助。我的解决方案效果很好,我的电子邮件提供商有问题。
你知道我如何扩展价值吗?我想插入多个值。
例如:
if ( '21079', '21080', '21081' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com';
这给我一个语法错误。
这也没有用:
if ( '21079' || '21080' || '21081' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com' ;
你的问题几乎是正确的。 set_properties()
需要传递整个数组(在您的情况下)$mailProp
.
/**
* Dynamically Change the recipient.
*
* @param object $contact_form The contact form 7 contact form object.
* @return void
*/
function cf7dynamicnotifications( $contact_form ) {
$submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class.
$posted_data = $submission->get_posted_data(); // Get all of the submitted form data.
// Make sure the field is filled in.
if ( isset( $posted_data['plz'] ) ) {
if ( '21079' === $posted_data['plz'] ) {
$recipient_email = 'office1@xyz.com';
} elseif ( '22085' === $posted_data['plz'] ) {
$recipient_email = 'office2@xyz.com';
} elseif ( '12345' === $posted_data['plz'] ) {
$recipient_email = 'office3@xyz.com';
} else {
$recipient_email = 'head-office@xyz.com';
}
// set the email address to recipient.
$mailProp = $contact_form->get_properties( 'mail' );
$mailProp['mail']['recipient'] = $recipient_email;
// update the form properties.
$contact_form->set_properties( array( 'mail' => $mailProp ) ); // Pass the whole array.
}
}
我们有 4 个办事处,分配了很多邮政编码:
- 21079, 22085,.... = 汉堡
- 10115, 10178,.... = 柏林
- 60306, 60312,.... = 法兰克福
- .......,......,.... = 慕尼黑
- 其他所有 = 总部
是否可以从表单中读出输入的邮政编码并将邮件动态发送到指定的办公室?
它必须是动态的,没有所有邮政编码或城市的下拉列表(在前端)。
我试过了没有成功
// hook into wpcf7_before_send_mail
add_action( 'wpcf7_before_send_mail', 'cf7dynamicnotifications'); // Hooking into wpcf7_before_send_mail
function cf7dynamicnotifications($contact_form) // Create our function to be used in the above hook
{
$submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class
$posted_data = $submission->get_posted_data(); // Get all of the submitted form data
if( $posted_data["plz"] == '21079' ) {
$recipient_email = 'office1@xyz.com';
}
elseif($posted_data["plz"] == '22085') {
$recipient_email = 'office2@xyz.com';
}
elseif($posted_data["plz"] == '12345') {
$recipient_email = 'office3@xyz.com';
}
else {
$recipient_email = 'head-office@xyz.com';
}
// set the email address to recipient
$mailProp = $contact_form->get_properties('mail');
$mailProp['mail']['recipient'] = $recipient_email;
// update the form properties
$contact_form->set_properties(array('mail' => $mailProp['mail']));
}
编辑
感谢您的帮助。我的解决方案效果很好,我的电子邮件提供商有问题。
你知道我如何扩展价值吗?我想插入多个值。
例如:
if ( '21079', '21080', '21081' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com';
这给我一个语法错误。
这也没有用:
if ( '21079' || '21080' || '21081' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com' ;
你的问题几乎是正确的。 set_properties()
需要传递整个数组(在您的情况下)$mailProp
.
/**
* Dynamically Change the recipient.
*
* @param object $contact_form The contact form 7 contact form object.
* @return void
*/
function cf7dynamicnotifications( $contact_form ) {
$submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class.
$posted_data = $submission->get_posted_data(); // Get all of the submitted form data.
// Make sure the field is filled in.
if ( isset( $posted_data['plz'] ) ) {
if ( '21079' === $posted_data['plz'] ) {
$recipient_email = 'office1@xyz.com';
} elseif ( '22085' === $posted_data['plz'] ) {
$recipient_email = 'office2@xyz.com';
} elseif ( '12345' === $posted_data['plz'] ) {
$recipient_email = 'office3@xyz.com';
} else {
$recipient_email = 'head-office@xyz.com';
}
// set the email address to recipient.
$mailProp = $contact_form->get_properties( 'mail' );
$mailProp['mail']['recipient'] = $recipient_email;
// update the form properties.
$contact_form->set_properties( array( 'mail' => $mailProp ) ); // Pass the whole array.
}
}