PHPMailer 不发送附件

PHPMailer not sending attachment

尝试用PHPMailer发送附件,发送正文,但没有发送附件。这是代码:

$filename2 = "$key.txt";
$filename = "$key.zip";
$file_path = dirname(__FILE__);
$myfile = fopen("$file_path/$filename2", "w") or die("Unable to open file!");
fwrite($myfile, $privkey);
fwrite($myfile, $pubkey);
fclose($myfile);
$zipFile = "$file_path/$filename";
$zipArchive = new ZipArchive();

if (!$zipArchive->open($zipFile, ZIPARCHIVE::OVERWRITE)){
    die("Failed to create archive\n");
}


$zipArchive->addGlob($filename2);
if (!$zipArchive->status == ZIPARCHIVE::ER_OK){
    echo "Failed to write local files to zip\n";
}

$zipArchive->close();

//Create Email to send to user with atachment
$message = "Test Email";
$subject = "Testing emails with attachment";

try
{
$email = new PHPMailer();
$email->setFrom('register@gasp.grn.cc', 'Gasp Bot');
$email->addReplyTo('no-reply@gasp.grn.cc', 'No-Reply');
$email->Subject   = $subject;
$email->Body      = $message;
$email->AddAddress($to,$username);
$email->AddAttachment($file_path, $filename);
$email->Send();
echo "Message has been sent";
}
catch(phpmailerException $e){
    echo $e->errorMessage();
}catch (Exception $e){
    echo $e->getMessage();
}
unlink ($zipFile);
unlink ("$file_path/$filename2");

我已经试过了

 $email->AddAttachment($zipFile);

但如果我使用它,消息正文也不会发送。

有人可以告诉我我犯的错误吗?

工作代码

$filename2 = "$key.txt";
$filename = "$key.zip";
$file_path = dirname(__FILE__);
$myfile = fopen("$file_path/$filename2", "w") or die("Unable to open file!");
fwrite($myfile, $privkey);
fwrite($myfile, $pubkey);
fclose($myfile);
$zipFile = "$file_path/$filename";
$zipArchive = new ZipArchive();

if (!$zipArchive->open($zipFile, ZIPARCHIVE::OVERWRITE)){
     die("Failed to create archive\n");
}


$zipArchive->addGlob($filename2);
if (!$zipArchive->status == ZIPARCHIVE::ER_OK){
   echo "Failed to write local files to zip\n";
}

$zipArchive->close();
try{
 //Create Email to send to user with atachment
 $message = "TEST MESSAGE";
$email = new PHPMailer();
$email->setFrom('register@gasp.grn.cc', 'Gasp Bot');
$email->addReplyTo('no-reply@gasp.grn.cc', 'No-Reply');
$email->Subject   = $subject;
$email->IsHTML(true);
$email->Body      = $message;
$email->AddAddress($to,$username);
$email->AddAttachment("$file_path/$filename");
if(!$email->Send()){
   echo "Message was not sent <p>";
   echo "Mailer Error: " . $email->ErrorInfo;
   exit;
}else{
   echo "Message has been sent";
}
}catch(phpmailerException $e){
    echo $e->errorMessage();
}catch (Exception $e){
    echo $e->errorMessage();
}
unlink ($zipFile);
unlink ("$file_path/$filename2");

正在快速添加

$email->IsHTML(true);

做到了

 $email->AddAttachment("$file_path/$filename");

有效