PHPMailer 密码保护的附件

PHPMailer password-protected attachment

我正在制作 PHP 脚本,从 mysql 数据库导入一些数据并通过电子邮件附件发送。但是有一个问题——将来有可能数据会变得敏感。所以我需要给附件设置密码。

我正在使用 phpmailer 方法 addStringAttachment

一些代码:

<?php
require 'PHPMailerAutoload.php';
$con = new mysqli("x", "x", "x", "x");


$utf1 = "SET NAMES utf8";
$utf2 = "SET character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8'";

$con->query($utf1);
$con->query($utf2);


$output ='';
$result = $con->query("select * from xxx WHERE DATE( entry_date ) BETWEEN ( DATE( CURDATE( ) - INTERVAL 3 DAY )) AND ( DATE( CURDATE( ) - INTERVAL 1 DAY ) )");
$columns_total = $con->field_count;


// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
    $heading = mysqli_fetch_field_direct($result, $i)->name;
    $output .= '"'.$heading.'";';
}

$output .="\n";

// Get Records from the table
while ($row = $result->fetch_array()) {
    for ($i = 0; $i < $columns_total; $i++) {
        $output .='"'.$row["$i"].'";';
    }
    $output .="\n";
}

$filename = "myFile.xls";
header('Content-Encoding: UTF-8');         

$bomoutput = "\xEF\xBB\xBF" . $output;

$con->close();

PHP邮寄者:

$mail = new PHPMailer;

[...]

$mail->From = 'xx';
$mail->FromName = 'xx';
$mail->addAddress('xx');     // Add a recipient

$mail->addStringAttachment($bomoutput, $filename);   

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

我在使用 UTF-8 字符时遇到了一些问题,所以我必须使用 UTF8 BOM。能给我一些吗advice/tip,附件安全级别最低怎么设置?它不知道如何加密它——但它必须如此。请给我一根鱼竿

这与 PHPMailer 或 MySQL 没有任何关系 - 它们会存储或发送您喜欢的任何内容,如果您想对其进行加密,那由您决定。

PHP 有几个函数可以让你加密字符串——但如果你想确保收件人能够轻松解密它,让它可用​​就有点复杂了——zip 文件可能是最简单的解决方案。

This question 有一些好的建议。