Jamol
Jamol

Reputation: 3918

send email with attachment with php mailer

Can anybody tell me steps to send email with attachment with phpMailer. I have prepared all codes to send email itself, and it is working fine but I did not understand how to send attachment. I have form and if I get value from a file input on POST it gives me only file name not full path. I think in order to addAttachment I need to get full path of file, right? I don`t have to store file on server, just need to send it with email.

Upvotes: 3

Views: 3405

Answers (1)

Jamol
Jamol

Reputation: 3918

I found the solution: Firstly, in my form I must include enctype="multipart/form-data" to send files, so it must be like this:

<form method="POST"  action="send.php" enctype="multipart/form-data">
          inputs...
</form>

After that I get file this way:

if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
            $file = $_FILES['myfile'];
            }

Then I prepare path to this file which is now in temp directory and identify new filename:

$attachment_path = $this->file['tmp_name'];

 //If need to give new name for the file:
  //$newfilename = pathinfo(basename($this->file['name']));
  //$attachment_name = "attachment_new_name.".$newfilename['extension'];

$attachment_name = basename($this->file['name']);

$mail->AddAttachment($attachment_path, $attachment_name);

Done! file is attached now.

Upvotes: 4

Related Questions