![](/img/trans.png)
[英]How to dynamically set email recipient on wordpress contact form 7 plugin
[英]Dynamic Email Recipient based on ZIP Code Input for Contact Form 7
我们有 4 个办公室,许多办公室分配了 ZIP 代码:
是否可以从表格中读出ZIP代码输入并动态发送邮件到指定办公室?
它必须是动态的,没有所有 ZIP 代码或城市的下拉列表(在前端)。
我试过了没有成功
// 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']));
}
编辑
谢谢您的帮助。 我的解决方案运行良好,但我的 email 提供商出现问题。
你知道我如何扩展价值吗? 我想插入多个值。
例如:
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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.