根据CF7中的下拉选择发送不同的电子邮件模板

问题描述 投票:0回答:1

我正在尝试制作一个表格,首先要求提供用户数据,然后发送一封包含下载我们目录的链接的电子邮件。该案例的特殊性在于,客户必须从下拉列表中选择其业务类型,并且由于目录不同,因此每个业务类型的电子邮件模板应该不同。这可以通过 CF7 表格来实现吗?

wordpress contact-form-7
1个回答
0
投票

有3种方法可以实现这一目标,

  1. 如果您不太擅长程序员,请为每种业务类型创建 1 个表单,这样每种类型就有 1 个电子邮件模板。然后,您可以在表单显示之前继续过滤业务类型,方法是将所有不同的表单实际上放在同一页面上,并仅显示与页面顶部的下拉选项相对应的表单。在页面加载时,仅显示下拉列表并隐藏所有表单,并且当下拉值更改(使用 js)时,您会动态显示适当的表单。该解决方案的缺点是您必须维护多种表单,但使其更易于理解。
  2. 您可以通过插件扩展(例如智能网格布局)上提供的通用邮件标签过滤器功能以编程方式实现此目的。该插件创建一个通用的
    mail_tag
    过滤器,例如
    [cf7sg-form-<your-form-name>]
    ,您可以将其插入邮件正文中,并且它允许您使用表单的所有提交字段以编程方式撰写邮件正文消息,以便您可以检查业务类型的值并相应地撰写邮件,(有关更多示例,请参阅此示例教程
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;
}
  1. 为了获得更大的灵活性,您还可以使用 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']; //this is the one you want to change.
  $components['additional_headers'];
  $components['attachments'];
  
  return $components;
}

这个论坛上有很多关于CF7 wpcf7_mail_components过滤器的

问题/答案

© www.soinside.com 2019 - 2024. All rights reserved.