Search code examples
phpphpmailersendmail

PHPMailer dont send the pdf attachment


my code create a pdf-file with fpdf that will safe in a folder called "bookings". After the file is saved, it should be send to the user by mail with phpmailer.

Actually the email will send correctly but the attachment is not send.

Here is the actual code, i try it now the whole day but i dont have any luck:

$filename = 'Buchung-Nr.' . $booking_id . '.pdf';
$path = $system->SETTINGS['siteurl'] . 'bookings/';
$outputfile = 'bookings/Buchung-Nr.' . $booking_id . '.pdf';
$pdfout = $pdf->Output($outputfile, 'F');

$bodytext = 'Vielen Dank für Ihre Buchung';

require $include_path . 'phpmailer/src/Exception.php';
require $include_path . 'phpmailer/src/PHPMailer.php';
require $include_path . 'phpmailer/src/SMTP.php';

$mail = new PHPMailer();
$replytotext = 'Ostsee-Deluxe - Buchung-Nr.' . $booking_id;
try {
    //Server settings
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'sslout.df.eu';                 //Set the SMTP server to send through
    $mail->SMTPSecure = "TLS";
    $mail->SMTPAuth   = true;
    $mail->CharSet = 'UTF-8';                              //Enable SMTP authentication
    $mail->Username   = 'USERNAME-HIDDEN';                     //SMTP username
    $mail->Password   = 'PW-HIDDEN';                               //SMTP password
    $mail->Port       = 25;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('[email protected]', 'Ostsee-Deluxe');
    $mail->addAddress($email, $fullname);     //Add a recipient
    $mail->addReplyTo('[email protected]', $replytotext);
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment($path/$filename);    //Optional name
    var_dump($path,$filename);
    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Buchungs-Nr. ' . $booking_id;
    $mail->Body    = $bodytext;
    $mail->AltBody = $bodytext_alt;

    $mail->send();
    
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

I can open the new created pdf attachment everytime in the folder, but the attachment in the email is empty.

Do you have any solutions?

EDIT: oh good idea, thanks for it i got it now! i changed:

$path = $system->SETTINGS['siteurl'] . 'bookings/'; to $path = 'bookings/';

and

$mail->addAttachment($path . $filename);    //Optional name

now it works, thank you @Akshay Hedge and Sven Eberth!!! I got some downvotes last year, but this time you all very kind for this basic question, keep it up! :)


Solution

  • I think

    $mail->addAttachment($path/$filename);
    

    should be

    $mail->addAttachment($path . $filename);
    

    Otherwise you divide the path by the filename.