How to Attach an Image Using PHPmailer

How can i attach an image using Phpmailer?

Per PHPMailer Manual, you sould use the method AddEmbeddedImage


$mail->AddEmbeddedImage(filename, cid, name);
By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');

like this:

$mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png");
$mail->Body = 'Embedded Image: <img alt="PHPMailer" src="cid:my-attach"> Here is an image!';

so cid:my-attach will be replaced with the inline-source of the image that's inside the email body

Send email with PHPMailer - embed image in body

I found the answer:

$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');

and on the <img> tag put src='cid:logo_2u'

How to embed image in phpmailer - I can't do it, why?

Add on the <img> tag put src='cid:Kartka'

$mail->AddEmbeddedImage('images/Kartka.png', 'Kartka');
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>";
"<p>This is a test picture: <img src=\"cid:Kartka\" /></p>";

Why you use so much \ ?? you can do it also like this :

<img src="cid:Kartka"/>

Send image attachment using phpmailer

You should only be firing the uploadImage function when there IS an $action:

...previous code...
if (empty($action))
{
?>
<form action=''>
<h1>header: </h1>
<label for='image'>upload: </label>
<input type='file' id='image' name='image' maxlength=50 >
</form>
<?php
exit; // stop the upload script running
}

$image = $_POST["image"];
uploadImage($image);
require("class.phpmailer.php");
... rest of the code ...

phpmailer sent inline images as attachment

If you attach images to an email, they are attachments, no matter what. If you use them by referring to their cid values from within your HTML, they will render inline as well, but they will still be shown as attachments, because they are. If you refer to images using remote URLs, they will render inline as well (providing you allow your mail client to do so), and they will not show as attachments - that is what the other messages you're seeing will be doing. Generally speaking, referring to images remotely is preferable to embedding/attaching.

Adding embedded images within mail body phpmailer class

Don't use $mailer->AddEmbeddedImage, but directly add

<img src="http://.../images/namDiams.png" /> instead.

The mail length should be lighter... And it works.

EDIT

I don't know if it will help you but there is a little mistake here :

$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png');

Should be

$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDiames.png');//the last param the second 'a' was missing...

Another topic here

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

Embed Image in Mail without having physical Image File

It always helps to read the docs. The function you're looking for is addStringEmbeddedImage, which does exactly what you want:

$URL = "http://maps.googleapis.com/maps/api/staticmap?center=Albany,+NY&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true";
$image = file_get_contents($URL);
$mail->addStringEmbeddedImage($image, 'staticMap', 'map.png', 'base64', 'image/png');
$mail->Body = '<img width="600" height="300" src="cid:staticMap">';

There are indeed pros and cons to using embedded images, but data urls have especially poor client compatibility, so are generally the worst option. Dynamically linking to the image may not be reliable with google maps, so you may want to proxy and cache the direct map lookups. This will also be much faster than hitting the API every time, and also more reliable as you can deliver an older version if the API doesn't respond or is slow.



Related Topics



Leave a reply



Submit