我正在开发一个 WordPress 项目,其中有一个联系表单 7,其中包含用于收集设备信息的字段。该表单允许用户动态添加多个设备。每个设备都有“名称”、“编号”、“生产日期”和“附加说明”等字段。
这是表单结构的简化版本:
<label>Device Name:
<input type="text" name="device_info[][name]" />
</label>
<label>Serial Number:
<input type="text" name="device_info[][number]" />
</label>
<label>Production Date:
<input type="date" name="device_info[][production_date]" />
</label>
<label>Additional Notes:
<textarea name="device_info[][notes]"></textarea>
</label>
我正在寻找一种解决方案来捕获动态添加的设备信息,将其附加到联系表 7 发送的电子邮件中,并将每个设备的详细信息作为单独的附件发送。这些字段是由 JS 脚本动态添加的(当单击按钮时,它会添加上面的 4 个字段)
我尝试使用 wpcf7_mail_sent 挂钩在提交后处理表单数据并将附件添加到电子邮件中。但是,我当前的实现似乎不起作用,因为电子邮件不包含设备详细信息。
任何有关如何实现此功能的帮助或指导将不胜感激!谢谢!
我想修改联系表 7 发送的电子邮件,以便它包含用户为每个设备提供的信息作为单独的附件。例如,如果用户使用三台设备提交表单,则电子邮件应包含三个动态生成的表单(有关 3 台设备的信息)。
您将需要拦截表单提交数据,处理设备信息,为每个设备创建单独的文本文件,然后将这些文件附加到邮件中。这是一个例子:
add_action('wpcf7_before_send_mail', function ($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
if (isset($posted_data['device_info'])) {
$devices = $posted_data['device_info'];
$attachments = array();
foreach ($devices as $device) {
$device_info = "{$device['name']}\n";
$device_info .= "{$device['number']}\n";
$device_info .= "{$device['production_date']}\n";
$device_info .= "{$device['notes']}";
$file = tempnam(sys_get_temp_dir(), 'device');
file_put_contents($file, $device_info);
$attachments[] = $file;
}
$mail = $contact_form->prop('mail');
if (isset($mail['attachments']) && !empty($mail['attachments'])) {
$mail['attachments'] .= "\n" . implode("\n", $attachments);
} else {
$mail['attachments'] = implode("\n", $attachments);
}
$contact_form->set_properties(array('mail' => $mail));
}
}
});