Add Attachment Through PHPmailer

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

Add attachment through PHPMailer

Change

$mail->MsgHTML();

to

$mail->Body;

Source: here

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

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

How to Add Multiple Attachments to an Email Using PHPMailer

This question was answered by synchro on another thread. https://github.com/PHPMailer/PHPMailer/issues/2098

"The threads you pointed at are years old!

The article about unique IDs is long obsolete; inline attachments with duplicate cid values will still be ignored, but that's expected behavior, and only applies to inline attachments created using addEmbeddedImage() and addStringEmbeddedImage().

The key problem here is that you're just not handling uploads properly. How you should handle uploads is covered in the PHP docs, and all that occurs before PHPMailer has any involvement.

First of all, you need to understand how file inputs work. These determine what shows up in the $_FILES superglobal that PHP populates for you. You can either have multiple file-type inputs that select a single file each, or you can have a single one that allows you to select multiple files. PHPMailer doesn't care either way, but you have to.

Next, you need to make sure you use move_uploaded_file or at least is_uploaded_file in order to validate what's in the $_FILES superglobal, otherwise, it's not safe.

Thirdly, you need to check whether the calls to addAttachment() are successful – at present, you're just assuming they work and have no error checking at all.

So, I recommend you take a look at the single and multiple file upload examples, both of which do all of the above, and there are no known problems with adding multiple attachments."

PHP - PHPMailer - how to add file as attachment with correct extension

@Hanoncs I managed to get it I think. The line:

$mail->addAttachment($file_destination, 'Uploaded file');

was renaming the file upon attachment to 'Uploaded file', literally. So, obviously, in the email there was no file extension on the file, because of the name. So all I had to do was change 'Uploaded file' to something with the extension, e.g. 'uploadedfile.ext' and it seemed to work. A silly, but enormously frustrating mistake, since all of the file upload handling seemed to work rather well. So the block now looks like this:

    // uploads/attaches file if there was one
if($have_file)
{
// give file unique name and set its destination as a temporary folder
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = sys_get_temp_dir() . '\\' . $file_name_new;

if (move_uploaded_file($file_tmp, $file_destination))
{
echo $file_destination;
$mail->addAttachment($file_destination, 'uploadedFile.dwg');
}
else
{
?>
<p class="text-center" style="margin-top:20px;font-size:16px;">Error sending message, please call and let us know</p>
<?php
}
}

Anyway, thanks for all your help, especially about the IDE, going to work on setting that up now.

Send attachments with PHP Mail()?

I agree with @MihaiIorga in the comments – use the PHPMailer script. You sound like you're rejecting it because you want the easier option. Trust me, PHPMailer is the easier option by a very large margin compared to trying to do it yourself with PHP's built-in mail() function. PHP's mail() function really isn't very good.

To use PHPMailer:

  • Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
  • Extract the archive and copy the script's folder to a convenient place in your project.
  • Include the main script file -- require_once('path/to/file/class.phpmailer.php');

Now, sending emails with attachments goes from being insanely difficult to incredibly easy:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

It's just that one line $email->AddAttachment(); -- you couldn't ask for any easier.

If you do it with PHP's mail() function, you'll be writing stacks of code, and you'll probably have lots of really difficult to find bugs.



Related Topics



Leave a reply



Submit