Search code examples
phpattachmentmailer

sending one attachment


I am sending a single attachment with an email message using the following code. I use the phpmailer class: I know it's a very simple and easy thing, but it is not working.

This is my php call;

$mail->AddAttachment($_FILES['attach']['name']);

This is my html;

<form id="Form" name="Form" method="POST" action="<?php $PHP_SELF ?>"  enctype="multipart/form-data">
<input id="attach" name="attach" type="file" />

I am sending an email with it too. The email is sent, but the attachment is not. Can anyone help me?


Solution

  • Encapsulate your code with error checking code so you know whether or not it worked:

    if (!$mail->AddAttachment($filepath, $_FILES['attach']['name']) {
      echo "Error attaching file at $filepath. Dumping variables to output.";
      var_dump($_FILES['attach']);
    }
    

    Note you can't simply pass the name of the file to the the AddAttachment method; the first parameter is expected to be a path, as per the PHPMailer API. You have to figure out what that path is, and then also include the name of the file.