Phpmailer Attachment

File attachment with PHPMailer

When you call

move_uploaded_file($file_tmp,"uploads/".$file_name);

This creates a file in the uploads/ directory with the name of the file as it was named on the uploader's computer.

Then you used sample code to add the attachment to phpMailer so you're basically attempting to attach non-existent files.

These two lines:

$mail->addAttachment('uploads/file.tar.gz');   // I took this from the phpmailer example on github but I'm not sure if I have it right.      
$mail->addAttachment('uploads/image.jpg', 'new.jpg');

should be changed to:

$mail->addAttachment("uploads/".$file_name);

Also note, it isn't necessary to call move_uploaded_file if you don't want to save the attachment after its uploaded and emailed. If that's the case just call AddAttachment with $_FILES['image']['tmp_name'] as the file argument.

Also, in your HTML form, you have

<input id="file" name="file" type="file" />

but refer to the input as image in the code. You should change the name of that input from file to image.

To only attach the image and not save it take out the move_uploaded_file code and add:

$file_tmp  = $_FILES['image']['tmp_name'];
$file_name = $_FILES['image']['name'];
//...
$mail->AddAttachment($file_tmp, $file_name);

Send File Attachment from Form Using phpMailer and PHP

Try:

if (isset($_FILES['uploaded_file'])
&& $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK
) {
$mail->addAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}

A basic example attaching multiple file uploads can be found here.

The function definition for addAttachment is:

/**
* Add an attachment from a path on the filesystem.
* Never use a user-supplied path to a file!
* Returns false if the file could not be found or read.
* Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client.
* If you need to do that, fetch the resource yourself and pass it in via a local file or string.
*
* @param string $path Path to the attachment
* @param string $name Overrides the attachment name
* @param string $encoding File encoding (see $Encoding)
* @param string $type MIME type, e.g. `image/jpeg`; determined automatically from $path if not specified
* @param string $disposition Disposition to use
*
* @throws Exception
*
* @return bool
*/
public function addAttachment(
$path,
$name = '',
$encoding = self::ENCODING_BASE64,
$type = '',
$disposition = 'attachment'
)

Add attachment through PHPMailer

Change

$mail->MsgHTML();

to

$mail->Body;

Source: here

TCPDF: PHPMailer attachment is empty

By using

$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');

require ('PHPMailer/PHPMailerAutoload.php');

$pdf_content = file_get_contents($fileatt);

You are trying to read a file_content from a String.

You can simply send the Attachment by

 $mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");  

Simple PDF file attachment in PHP Mailer

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');

Email Attachment from directory is not always the latest using PHPMailer

PHP's dir() function returns items in an indeterminate order. If you want to retrieve files ordered by date, see this answer.

PHPMailer dont send the pdf attachment

I think

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

should be

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

Otherwise you divide the path by the filename.



Related Topics



Leave a reply



Submit