0

I have this PHP code to attach a document to an email:

require('./PHPMailer_v5.1/class.phpmailer.php');

$mail = new PHPMailer();
$mail->Username = "xxxxx.com";
$mail->Password = "xxxx";
$mail->Host = "smtp.office365.com";
$mail->Mailer = "smtp";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->SetFrom("xxxxx.com", "Pay Slip");
$mail->AddReplyTo("xxx", "Pay Slip");
$filename = $_SERVER['DOCUMENT_ROOT'] . '/' . 'Payslip/admin/salaryslip' . '/' . $row['year'];
$fullfilepath='employyee.pdf'
$filename2 = $filename . '/' . $row['month'] . '/' . $fullfilepath;
   
$mail->AddAttachment($filename2, $subject, $encoding = 'base64', $type = 'application/pdf');

The email sends successfully with the attachment, but the .pdf extension is missing in the received mail. Any help would be appreciated.

ADyson
  • 57,178
  • 14
  • 51
  • 63
RAMIA M K
  • 1
  • 1
  • $fullfilepath='employyee.pdf' .....Is this a typo employee ? – Indra Kumar S May 27 '21 at 14:33
  • which library are you using? PHPMailer perhaps? Add some context to your question please. – ADyson May 27 '21 at 14:38
  • @ADyson sorry..I have update the question.Please help me – RAMIA M K May 27 '21 at 14:42
  • Thanks, that's useful. `$subject` is not defined anywhere in your code, so we don't know its content or how it's populated, but that's what you're passing as the final filename for the attachment. If that doesn't contain .pdf on the end, then obviously neither will the attachment when it's received. – ADyson May 27 '21 at 14:48
  • 1
    You're using an ancient, buggy and vulnerable version of PHPMailer – upgrade! You have some strange syntax too: `$mail->AddAttachment($filename2, $subject, $encoding = 'base64', $type = 'application/pdf');` should be just `$mail->AddAttachment($filename2, $fullfilepath);`; encoding and MIME type will be correct using defaults. – Synchro May 27 '21 at 15:13

1 Answers1

1
$mail->AddAttachment($filename2, $subject, $encoding = 'base64', $type = 'application/pdf');

As per the documentation, your second param should be a valid file name.

$mail->addAttachment($filename2, 'employee.pdf')

enter image description here

Refer : PHPMailer Documentation

Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27