You need to enable JavaScript to run this app. CF7生成CSV邮件附件时逗号破坏格式问题如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

CF7生成CSV邮件附件时逗号破坏格式问题如何解决?

问题修复方案

根因说明

你目前的CSV是手动拼接字符串生成的,没有对字段内容里的逗号、双引号等CSV特殊字符做转义处理,一旦用户输入包含逗号就会导致列错位。

修复方案

直接使用PHP原生的fputcsv()函数生成CSV,该函数会自动处理所有特殊字符:给包含逗号/空格/引号的字段自动包裹双引号,同时转义字段内部的双引号,完全符合标准CSV格式规范。

修改后的完整代码

//Create the shortcode which will set the value for the DTX field
function cf7dtx_counter(){
    $val = get_option( CF7_COUNTER, 0) + 1;  //Increment the current count
    return $val;
}
add_shortcode('CF7_counter', 'cf7dtx_counter');

//Action performed when the mail is actually sent by CF7
function cf7dtx_increment_mail_counter(){
    $val = get_option( CF7_COUNTER, 0) + 1; //Increment the current count
    update_option(CF7_COUNTER, $val); //Update the settings with the new count
}
add_action('wpcf7_mail_sent', 'cf7dtx_increment_mail_counter');

// Add this to functions.php //
// Create CSV by scraping email using before_send_mail action //
add_action( 'wpcf7_before_send_mail', 'cf7_csv_creation' );
function cf7_csv_creation($cf7) {
    // Allow to target the ID of specific form  //
    $form_id = $cf7->id();
    // Check certain form by ID - remove this IF statement if not specific //
    // Make sure the file is saved into wp-content to retieve it within WPCF7 settings as an attachement //
    $user_register_csv = 'wp-content/uploads/csvs/csvreserva.csv';
    
    // 以写入模式打开文件
    $fp = fopen($user_register_csv, 'w');
    
    // 写入CSV表头,顺序和你原来的一致
    $headers = [
        "poblacio","tipus","data-entrada","data-sortida","PrimerCognom","SegonCognom","nom","Sexe","data-naixament","identificacio","Adreca","CodiPostal","Ciutat","Provincia","Pais","Telefon","Mobil","email","Estudis","Curs","Centre","Familiar","NIF-familiar","professio-familiar","Adreca-familiar","CodiPostal-familiar","Poblacio-familiar","Provincia-familiar","Pais-familiar","Mail-familiar","Telefon-familiar","Mobil-familiar","questionari-coneixament","questionari-aficions","accepto","num-registre","maildesti","titol","url","arxiu-dni","arxiu-universitat","arxiu-pagament","arxiu-dni-url","arxiu-universitat-url","arxiu-pagament","url","Submitted From","data","accepta-politica","ip","navegador","your-email"
    ];
    fputcsv($fp, $headers);
    
    // 按表头顺序组装提交的字段数据
    $row = [
        $_POST["residencia"] ?? '',
        $_POST["menu-147"] ?? '',
        $_POST["date-5"] ?? '',
        $_POST["date-6"] ?? '',
        $_POST["text-145"] ?? '',
        $_POST["text-4445"] ?? '',
        $_POST["text-144"] ?? '',
        $_POST["radio-603"] ?? '',
        $_POST["date-34"] ?? '',
        $_POST["text-146"] ?? '',
        $_POST["text-147"] ?? '',
        $_POST["text-1478"] ?? '',
        $_POST["text-149"] ?? '',
        $_POST["text-150"] ?? '',
        $_POST["menu-950"] ?? '',
        $_POST["text-151"] ?? '',
        $_POST["text-1521"] ?? '',
        $_POST["email-775"] ?? '',
        $_POST["text-200"] ?? '',
        $_POST["text-201"] ?? '',
        $_POST["text-202"] ?? '',
        $_POST["text-301"] ?? '',
        $_POST["text-302"] ?? '',
        $_POST["text-313"] ?? '',
        $_POST["text-303"] ?? '',
        $_POST["text-304"] ?? '',
        $_POST["text-305"] ?? '',
        $_POST["menu-951"] ?? '',
        $_POST["text-306"] ?? '',
        $_POST["text-309"] ?? '',
        $_POST["text-308"] ?? '',
        $_POST["text-3048"] ?? '',
        $_POST["text-543"] ?? '',
        $_POST["text-697"] ?? '',
        $_POST["Si"] ?? '',
        $_POST["cf7-coutner"] ?? '',
        $_POST["reserves@sample.com"] ?? '',
        $_POST["Formulario recibido"] ?? '',
        $_POST["https://www.sample.com"] ?? '',
        $_POST["file-628"] ?? '',
        $_POST["file-629"] ?? '',
        $_POST["file-630"] ?? '',
        $_POST[""] ?? '',
        $_POST[""] ?? '',
        $_POST[""] ?? '',
        $_POST["1"] ?? '',
        $_POST["_date"] ?? '',
        $_POST["Si"] ?? '',
        $_POST["_remote_ip"] ?? '',
        $_POST["_user_agent"] ?? '',
        $_POST["your-email"] ?? ''
    ];
    // 写入单行数据,自动处理特殊字符转义
    fputcsv($fp, $row);
    
    // 关闭文件句柄
    fclose($fp);
}
// Clear file/user data after submission //
add_action('wpcf7_mail_sent', function ($cf7) {
    $user_register_csv = 'wp-content/uploads/csvs/csvreserva.csv';
    file_put_contents($user_register_csv, '');
    // File cleared and ready to be rewritten on next submission //
});
// Remember - add the above path to the WPCF7 File attachment setting within the relevant form //

注意事项

  • 请先确认wp-content/uploads/csvs目录存在且服务器有写入权限,避免文件生成失败
  • 代码中使用了??运算符避免字段未提交时报PHP警告,兼容PHP7.0及以上版本,若你的服务器PHP版本更低,可替换为isset($_POST['xxx']) ? $_POST['xxx'] : ''
  • 你原代码中存在几个键为空的$_POST[""],以及重复的url、arxiu-pagament字段,请自行核对和CF7表单的实际字段对应关系,调整字段顺序即可。

内容的提问来源于stack exchange,提问作者Marc Lara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 06:06:12