tags:

views:

140

answers:

4

Hi, I was just wondering if I could have a variable to hold an image, I'm using phpmailer to send email and I need an image to be attached to it,

so I was wondering if I could put the image in a variable and use

$mailer->AddAttachment($image);

to send the email with attachment.

thanks for your help.

+1  A: 

I guess $image should contain local path to the image file.

If you look at phpMailer source, at line 1218: http://phpmailer.svn.sourceforge.net/viewvc/phpmailer/phpmailer/trunk/class.phpmailer.php?revision=444&view=markup you'll see that it verifies at first that what you have given is path to existing file. There is no other option.

Kamil Szot
A: 

Unless I'm missing something, that's exactly how it's supposed to be used.

According to this document, you'd do something like this:

$myImg = '/some/path/to/image.jpg';
$mailer->AddAttachment($myImg);

Is that not what you're trying?

inkedmn
yes that's what I did and I was getting errors so I thought maybe I'm doing sth wrong.thanks
amir
My magic 8 ball is out of order... *What* errors?
Daniel
+1  A: 

With PhpMailer adding an attachment is done the way you wrote it in the question

$mailer->AddAttachment('/home/mywebsite/file.jpg', 'file.jpg');

If you want to use a variable you can change the string by a variable without problem.

$imagePath = '/home/mywebsite/file.jpg';
imageName = 'file.jpg'
$mailer->AddAttachment($imagePath, $imageName);
Daok
A: 

Why cant you do this this way? Sending email attachments in PHP Using phpmailer class !

Shoban