File Attachment with 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);

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

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

phpmailer file attachment delivered, but downloads a string only

Here are two places to start troubleshooting:

1. Attaching of File

Instead of this:

$mail->AddStringAttachment()

try this:

$mail->AddAttachment()

File Attachments

The command to attach a local file is simply
$mail->addAttachment($path);, where $path contains the path to the
file you want to send, and can be placed anywhere between $mail = new PHPMailer; and sending the message. Note that you cannot use a URL
for the path
- you may only use local filesystem path.

If you want to send content from a database or web API (e.g. a remote PDF generator), do not use this method - use addStringAttachment instead.


2. MIME type

Instead of this:

application/pdf

try this:

image/vnd.dxf

List of MIME types: http://www.freeformatter.com/mime-types-list.html

phpmailer & ajax : file attachment with the mail

You form tag doesn't contain all attributes.

Replace

<form id="contact-form" method="post" action="/handler.php?ajax=contact">

with

<form id="contact-form" method="post" action="/handler.php?ajax=contact" accept-charset="utf-8" enctype="multipart/form-data">

$(document).ready(function() {$('#contact-form').submit( function( e ) {var formData = new FormData($(this)[0]);
$.ajax({ url: window.location.pathname, type: 'POST', data: formData, async: false, success: function (data) { alert(data) }, cache: false, contentType: false, processData: false }); return false; });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form id="contact-form" method="post" action="/handler.php?ajax=contact" accept-charset="utf-8" enctype="multipart/form-data">
<input type="email" name="contact-email" id="contact-email" />
<select name="contact-category" id="contact-category"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select>
<input type="text" name="contact-subject" id="contact-subject" />
<textarea name="contact-message"></textarea>
<input type="file" name="contact-attachment" id="contact-attachment" />
<button class="btn-classic btn-orange"><i class="fa fa-paper-plane"></i> send</button>
</form>

Sending File Attachment with PHPMailer in Slim php framework

use Psr\Http\Message\UploadedFileInterface; //slimphp File upload interface
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$app = new \Slim\App;

$app->post('/send_mail_Attachment', function ($request, $response){
//get the file from user input
$files = $request->getUploadedFiles();
$uploadedFile = $files['fileName']; //fileName is the file input name
$filename = $uploadedFile->getClientFilename();
$filesize = $image->getSize();

//check file upload error
if ($uploadedFile->getError() != UPLOAD_ERR_OK) {
//return your file upload error here
}

//Check file size
if ($filesize > 557671) {
//return error here if file is larger than 557671
}

//check uploaded file using hash
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $filename));

//upload file to temp location and send
if (move_uploaded_file($uploadedFile->file, $uploadfile)){

//send email with php mailer
$mail = new PHPMailer(true);

try{
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.zoho.com'; //change to gmail
$mail->SMTPAuth = true;
$mail->Username = 'admin@yourdomain.com';
$mail->Password = 'your password here';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('admin@yourdomain.com', 'Web Admin');
$mail->addAddress('email address your sending to');

$mail->isHTML(true); //send email in html formart
$mail->Subject = 'email subject here';
$mail->Body = '<h1>Email body here</h1>';
$mail->addAttachment($uploadfile, $fileName); //attach file here
$mail->send();

//return Email sent
}
catch (Exception $e) {
$error = $mail->ErrorInfo;
//return error here
}
}

//return error uploading file

});

How can I Attach a .eml File Using PHPMailer?

This did the trick:

$mail->addStringAttachment($body, 'Original Email.eml', 'base64', 'application/octet-stream');


Related Topics



Leave a reply



Submit