thestoneoflapiz
thestoneoflapiz

Reputation: 55

Simple PDF file attachment in PHP Mailer

I'm trying the simplest way to send an -email- with -an attached file- using PHP mailer.

I tried using this

   $mail ->addAttachment("path_to_pdf", "pdf_name);

but it does't work, since the "Mail is sent" but the "PDF file attachment" is not sent.

Please help me solve my problem, I want to attach a pdf file in the email I want to send to the receiver. Thanks!

Here are the files I used to send email w/file attachment.

the index.html

        <html>
        <head>
        </head>
        <body>
            <form method="post" action="send_mail.php" enctype="multipart/form-data">
            To : <input type="text" name="mail_to"> <br/>
            Subject :   <input type="text" name="mail_sub">
           <br/>
             Message   <input type="text" name="mail_msg">
             <br/>
            File: <input type="file" name="file" >
            <br/>
                <input type="submit" value="Send Email">

            </form>
        </body>
    </html>

and

send_mail.php

      <?php

        $mailto = $_POST['mail_to'];
        $mailSub = $_POST['mail_sub'];
        $mailMsg = $_POST['mail_msg'];
       require 'PHPMailer-master/PHPMailerAutoload.php';
       $mail = new PHPMailer();
       $mail ->IsSmtp();
       $mail ->SMTPDebug = 0;
       $mail ->SMTPAuth = true;
       $mail ->SMTPSecure = 'ssl';
       $mail ->Host = "smtp.gmail.com";
       $mail ->Port = 465; // or 587
       $mail ->IsHTML(true);
       $mail ->Username = "[email protected]";
       $mail ->Password = "accountsamplepassword";
       $mail ->SetFrom("[email protected]");
       $mail ->Subject = $mailSub;
       $mail ->Body = $mailMsg;
       $mail ->AddAddress($mailto);
        $mail->AddAttachment('pdf_files/', 'reservation.pdf');


       if(!$mail->Send())
       {
           echo "Mail Not Sent";
       }
       else
       {
           echo "Mail Sent";
       }

    ?>

Please help me with my problem, Thanks!

Upvotes: 2

Views: 14455

Answers (1)

Jakumi
Jakumi

Reputation: 8374

this line doesn't do what you'd expect

$mail->AddAttachment('pdf_files/', 'reservation.pdf');

it tries to find a file named 'pdf_files/' and wants to add it. however, as you might imagine now, this isn't a proper file name. The first argument of AddAttachment is the path of the file (that is the including the filename of the file), the second parameter is the filename, which is shown in the email, how the file is supposed to be called/named, so you can call it differently, without renaming the original file.

so the line above should probably read:

$mail->AddAttachment('pdf_files/reservation.pdf', 'reservation.pdf');

Upvotes: 2

Related Questions