英文:
Sent different email templates based on dropdown selections in CF7
问题
我正在尝试制作一个表单,首先要求用户提供数据,然后发送一封带有下载我们目录链接的电子邮件。这个案例的特殊之处在于,客户必须从下拉菜单中选择他的业务类型,并且每个业务类型的电子邮件模板应该是不同的,因为目录是不同的。是否可以通过CF7表单实现这个目标?
英文:
I'm trying to make a form that asks for user data first and then sends an email with a link to download our catalog. The particularity of the case is that the customer have to select his business type from a dropdown and the email template should be different for each one since the catalogs are different. Can this be achieved with a CF7 form?
答案1
得分: 1
有三种方法可以实现这个目标:
-
如果你不是很擅长编程,那么可以为每种业务类型创建一个表单,这样每种类型都有一个电子邮件模板。然后,在表单显示之前,可以通过在同一页上放置所有不同的表单,并只显示与页面顶部下拉选择相对应的表单来筛选业务类型。在页面加载时,只显示下拉选择框,隐藏所有表单,当下拉选择框的值发生变化时(使用js),动态显示相应的表单。这种解决方案的缺点是需要维护多个表单,但更容易理解。
-
你可以通过插件扩展(如Smart Grid-layout)提供的通用邮件标签过滤功能来以编程方式实现。该插件创建了一个名为
[cf7sg-form-<your-form-name>]
的通用mail_tag
过滤器,你可以将其插入到邮件正文中,并可以使用表单的所有提交字段以编程方式组合邮件正文消息,因此你可以检查业务类型的值并相应地组合邮件(查看此示例tutorial获取更多示例)。
add_filter( 'cf7sg_mailtag_cf7sg-my-custom-form', 'filter_cf7_mailtag_cf7sg_form_my_custom_form', 10, 3);
function filter_cf7_mailtag_cf7sg_form_my_custom_form($tag_replace, $submitted, $cf7_key){
/*要更改的$tag_replace字符串*/
/*$submitted是一个包含所有提交字段的数组*/
/*$cf7_key是一个唯一的字符串键,用于标识你的表单,在仪表板中的表单表中可以找到。*/
if('my-custom-form'==$cf7_key ){
switch($submitted['business-type']){
case 'type1':
$body = ... //type1业务的消息正文。
break;
case 'type2':
$body = ... //type2业务的消息正文。
break;
default:
$body = "未知业务"。
break;
}
return $body;
}
- 为了更灵活,你还可以使用CF7插件的邮件组合钩子
wpcf7_mail_components
以编程方式实现。该钩子允许你过滤插件用于创建和发送通知邮件的所有组件。
add_filter('wpcf7_mail_components', 'filter_cf7_mail_components',10,3);
function filter_cf7_mail_components($components, $form_obj, $wpcf7_mail_object){
$components['subject'];
$components['sender'];
$components['body']; //这是你想要更改的部分。
$components['additional_headers'];
$components['attachments'];
return $components;
}
关于CF7 wpcf7_mail_components
过滤器,这个论坛上有很多问题/答案。
英文:
There are 3 ways to achieve this,
- If you are not much of a programmer, then create 1 form for each business type, so you have 1 email template for each type. You can then proceed to filter the business type prior to the form display, by actually having all the different forms on the same page and only showing the one that corresponds to a dropdown selection at the top of the page. At page load only the dropdown is shown and all forms are hidden, and when a dropdown value changes (using js) you show the appropriate form dynamically. The downside of this solution is that you have to maintain multiple forms, but makes it simpler to understand.
- You can programmatically achieve this with the general mail tag filter functionality offered on a plugin extension such as the Smart Grid-layout. The plugin creates a general
mail_tag
filter such as[cf7sg-form-<your-form-name>]
which you can insert into the body of your mail, and it allows you to programmatically compose your mail body message using all the submitted fields of your form, so you can check the value of the business type and compose the mail accordingly, (see this example tutorial for more examples)
add_filter( 'cf7sg_mailtag_cf7sg-my-custom-form', 'filter_cf7_mailtag_cf7sg_form_my_custom_form', 10, 3);
function filter_cf7_mailtag_cf7sg_form_my_custom_form($tag_replace, $submitted, $cf7_key){
/*the $tag_replace string to change*/
/*the $submitted an array containing all submitted fields*/
/*the $cf7_key is a unique string key to identify your form, which you can find in your form table in the dashboard.*/
if('my-custom-form'==$cf7_key ){
switch($submitted['business-type']){
case 'type1':
$body = ... //the message body for type1 businesses.
break;
case 'type2':
$body = ... //the message body for typee businesses.
break;
default:
$body = "Unknown business".
break;
}
return $body;
}
- For more flexibility, you can also achieve this programmatically with the CF7 plugin's mail composition hook
wpcf7_mail_components
which allows you to filter all the components used by the plugin to create and send the notification mail,
add_filter('wpcf7_mail_components', 'filter_cf7_mail_components',10,3);
function filter_cf7_mail_components($components, $form_obj, $wpcf7_mail_object){
$components['subject'];
$components['sender'];
$components['body']; //this is the one you want to change.
$components['additional_headers'];
$components['attachments'];
return $components;
}
There are quite a few questions/answers on the CF7 wpcf7_mail_components
filter on this forum.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论