Search code examples
phppdfphpmailer

pdf not sending as attachment


I have created a php send email script to sending pdf via php mailer along with the attachments I am unable to send as attachments when I am sending an email the attached file is broken it's ok please have a look at my code and let me know what mistake I am doing why the attachments are not sending properly

Here is my html

<form method="POST" action="details.php?id=<?php echo $_GET['id']; ?>" enctype="multipart/form-data">
    <div class="col col_center">
        <input name="first_name" class="firstname text_input" type="text" placeholder="First Name">
    </div>
    <div class="col col_center">
        <input name="last_name" class="lastname text_input" type="text" placeholder="Last Name">
    </div>
    <div class="col col_center">
        <input name="email" class="email_address text_input" type="email" placeholder="Email Address">
    </div>
    <div class="col col_center">
        <input name="phone" class="phone text_input" type="tel" placeholder="Phone (with country code)">
    </div>
    <input type="hidden" name="title" value="<?php echo $dt['job_title']; ?>" />
    <div class="btn_row">
        <input type="file" value="Attach CV" class="button blue" name="resume" style="width:auto;">
    </div>
    <div class="btn_row">
        <input type="submit" value="Send" name="submit_resume" class="button" style="width:auto;">
    </div>
</form>

This is my php file

$path     = "./uploads/";
    $head     = $_FILES["resume"]["name"];
    $headtype = $_FILES["resume"]["type"];
    $headtemp = $_FILES["resume"]["tmp_name"];

    move_uploaded_file($headtemp, $path.$head);

 $mail = new PHPMailer;
    $client_email = $dt[3];
    $mail->setFrom('[email protected]', 'No reply');
    $mail->addAddress("$client_email", 'Xpertius');
    $mail->Subject = "Thank You For Appling - '".$job_title."'";
    $mail->msgHTML($htmlbody);
    $uploadfile1 = tempnam(sys_get_temp_dir(), sha1($_FILES['resume']['name']));
    move_uploaded_file($_FILES['resume']['tmp_name'], $uploadfile1);
    $mail->addAttachment($uploadfile1, $head);

I have also tried to save it to y database and the file is saving properly but not sending as attached file link is broken in the email


Solution

  • You try to move your uploaded file:

    move_uploaded_file($_FILES['resume']['tmp_name'], $uploadfile1);
    

    But you have already moved it in:

    move_uploaded_file($headtemp, $path.$head);
    

    Most probably, the file you try to attach is empty (as is no longer exists), check the size in email, or check before attaching if it still exists.

    Therefore, you should define $uploadfile1 as:

    $uploadfile1 = $path.$head;
    

    in place of your second move_uploaded_file.