Search code examples
phpemailphpmaileremail-attachments

PHPMailer not sending attachment


Trying to use PHPMailer to send an attachment, the body is sent but the attachment is not. Here is the code:

$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");

I already tried

 $email->AddAttachment($zipFile);

but if I use this the body of the message is also not sent.

Can somebody show me the error I'm making?


Solution

  • Working Code

    $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");
    

    aperantly adding

    $email->IsHTML(true);
    

    made it so that

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

    works