How to prevent phpmailer sending embedded image as an attachment on Gmail? - php

I am sending an email with the logo of a company in it. However, I find it annoying when it shows the .png file on the list of emails on Gmail. Upon clicking the email, the logo is there and there's no attachment (which is good). But I really need to remove the file when viewing on the email list. See the image below.
is there a way where I can remove this "email-logo.png"?
.
.
$mail->AddEmbeddedImage('img/email-logo.png','logo');
.
.
$mail->Body = "<img src='cid:logo'>";
.
.

You can try using
$mail->clearAttachments();

I resolved to putting the src attribute of the image to a remote link e.g src="http://somesite.com/image.png". I resolved to this since I always saw embedded images always coming as attachments as you have mentioned. But some clients block remote content as I have seen with this.

Related

Laravel : image with base64 source ,not able to view in email

I am working with laravel below is steps which I have followed,
I am using editor for emails to user,
from editor I have upload image,
that image is uploaded as base64 format
I am sending that editor content in email to user
I have checked that base64 string is correct and showing me image
also able to display that image with my system
but when I am going to send that image in email ,
that image is not visible for me.
I have checked there in image src attribute, there is showing like -
<img style="width:224px">
in gmail ,here src attribute is removed ,but I have checked that tag befor send email , there was src tag with proper base64 encoded string
Please help me to show image in email ,
To send mail I am using laravel own configuration for email as below,
use App\Events\SendMail;
$mailData['toName'] = $toUserFullname;
$mailData['toEmail'] = $toEmail;
$mailData['fromName'] = $fromName;
$mailData['fromEmail'] = $fromEmail;
$mailData['emailSubject'] = $templateData['email_subject'];
$mailData['emailContent'] = $templateData['email_body'];
\Event::fire(new SendMail($mailData));
Base64 encoded images are not well supported in emails. They aren't supported in most web email clients (including Gmail) and are completely blocked in Outlook.
So the first solution that comes to my mind is to create actual image file from base64 string before sending email. Something like that colud help:
$filename_path = md5(time().uniqid()).".jpg";
$decoded=base64_decode($base64_string_img);
file_put_contents("path/to/save/image/".$filename_path,$decoded);
You also shouldn't forget to unlink this images in case you don't need them after.
You can also try using inline attachemnts.
<img src="{{ $message->embedData($data, $name) }}">

png attachment generated by an API in phpmailer

I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!

How to specify an email drop location

I'm using Drupal CMS on top of PHP and IIS. When I send emails containing embed images, the images are not displayed in Outlook.
I'm trying to isolate the problem.
There is nothing in Drupal or Outlook which will allow me to view the complete message body with headers.
Is there a way to configure PHP to write the email to a folder on disk instead of sending the email?
you can write it to a text file instead of sending, just need to find the place where it happens:
$folder=dirname(__FILE__)."/emaildir";
$txtfilename=time().'.txt';
$emailstr=$header . "/n" . $message . "/n";
instead of mailto() or whatever function just write to file
$fh=fopen($txtfilename,"w");
$fwrite($fh,$emailstr);
fclose($fh);
This is written from my head, you might want to check for mistakes but you get the picture

add inline image to a message sent with swiftmailer

Please excuse my php but, Im using Swiftmailer to send emails from a clients website. They've requested to add an image or two as a signature etc and so looking at the swiftmailer spec here
http://swiftmailer.org/docs/messages.html
They suggest either adding an inline image like this
$message->embed(Swift_Image::fromPath('http://site.tld/image here'))
or like this(in 2 steps)
$cid = $message->embed(Swift_Image::fromPath('image here'));
then in the emails body section add
<img src="' . $cid . '" alt="Image" />'
Both steps ive tried but to no avail. When i hit the send email button, i get this error which i dont quite know what to make of it.
Call to a member function embed() on a non-object in /home/content/78/5152878/html/4testing/erase/ask-doc-proc2.php on line 89
The only thing i added to my already working code and email was the image code directly from the example in the docs pages. This error obviously prevents the email from being sent. if i remove it then it sends emails fine. Since i need to add an image to this,
Any help is greatly appreciated. Thank you
edit: this is the portion where the email is built and sent
$cid= $message->embed(Swift_EmbeddedFile::fromPath('http://myforecyte.com/dev/pic.jpg'));
->setTo( $docEmail)
->setBody("Hello" . "\r\n\r\n" .
$fullName . " has visited MyForeCYTE.com. Upon their visit they have requested to learn more about the test. \r\n\r\n" .
"Please visit www.ClarityWomensHealth.com to find out more about The ForeCYTE Breast Health Test, or call our customer support line at 1 (877) 722-6339. \r\n\r\n" .
"We look forward to hearing from you. \r\n\r\n" .
"Thank You," , 'text/plain')
->addPart("Hello" . ",</b><br/><br/>" .
"<b>" . $fullName . "</b> has visited www.MyForeCYTE.com. Upon their visit they have requested to learn more about the test. <br/>" .
"Please visit www.ClarityWomensHealth.com to find out more about The ForeCYTE Breast Health Test, or call our customer support line at 1 (877) 722-6339.<br/> " .
"We look forward to hearing from you. <br/><br/><br/>" . "<img src='" . $cid. "' alt='pic'/>" .
"Thank you " , 'text/html')
;
After all the running around i found an alternate solution. Swiftmailer allows 2 methods in which to perform the same thing.
one is the embed() function
and the other one is the attach() function
so to the code above, i removed the "embed()" since it wasn't working for me and added these 2 lines below and it works
->attach(Swift_Attachment::fromPath('path to image here.jpg')
->setDisposition('inline'));
and it worked 100%
None of the answers really worked for me. I had to include inline images, using CID. What I had to do, to make it work:
$attachment = Swift_Image::newInstance($data, $filename, $mimeType)
->setDisposition('inline');
$cid = $message->embed($attachment); // Generates "cid:something"
Important part is using Swift_Image class. Then the image in html should be:
<img src="cid:something" ... />
I think this solution works without doing something hacky with swiftmailer (ver. 5.4.2). It is exactly how the documentation says.
Remember to test multiple email clients (gmail, thunderbird, apple mail, web clients...) if the inline images work. For example using Swift_Attachment, inline images showed in gmail, but not in some web clients. Using Swift_Image, it worked everywhere.
The accepted answer doesn't work (version tested: 5.4.2).
(Mine works but could be perfected)
Instead, looking inside the "Original" (Gmail: Show Original) I've found that swiftmailer is omitting adding 2 headers to the attachment, namely:
Content-ID: <ABC123>
X-Attachment-Id: ABC123
ABC123 is the cid we have to put in the body where we want the inline to be showed:
So thanks to this question: I found the way to fix it for swiftmailer (that is even against swiftmailer documentation, but it works while theirs do not)
this is the final (ugly) code:
$attachment = Swift_Attachment::fromPath('image.jpg')->setDisposition('inline');
$attachment->getHeaders()->addTextHeader('Content-ID', '<ABC123>');
$attachment->getHeaders()->addTextHeader('X-Attachment-Id', 'ABC123');
$cid = $message->embed($attachment);
$img = '<img src="cid:ABC123"/>';
$html = "
<html>
<head>
</head>
<body>
$img
</body>
</html>
";
$message->setBody($html, 'text/html');
I know this is an old question (with an accepted answer), but for anyone having the same problem, the Swift_Image::fromPath($image_path) method expects the image path to be local (a filesystem path):
Swift_Image::fromPath("/local/path/to/image.jpg");
// Local path = great success
Thus the below will fail if your image path is external (starting with https://, etc.):
Swift_Image::fromPath("https://external.path/to/image.jpg");
// External path = epic fail
For external images, you should use:
# Load the image file
$data = file_get_contents($image_path);
# Get the filename
$filename = explode("?",basename($image_path))[0];
# Get the mime type
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime_type = $finfo->buffer($buffer);
# Load the image file
$image_file = new \Swift_Image($data, $filename, $mime_type);
# Set the disposition to inline
$image_file->setDisposition('inline');
If you use laravel, try to add public_path() at the beginning of the path, e.g.:
<img src="<?php echo $message->embed(public_path().'/img/backend/name.jpg); ?>">

php image display without download in mail

hi i want to send a image in html format using php mailer class but image show in mail after downloading. but i want to display the image without downloading. is there any option in mailer class or there is another method for this.
or i have to send the the image in another format.
Well, there can be only two possible answers:
you do not want to embed the actual image file with the eMail, then simply put an <img> element into the eMail linking to the image at the remote location, just like you would with any other HTML page. Then cross fingers and hope the client has HTML email enabled and allows display of remote images.
or
you dont want to reference the file from a remote server, but embed it with the eMail. In that case, refer to How To Embed Images in HTML EMail or Attaching an image to an email
If your using PHP Mailer...
$mail = new PHPMailer();
$mail->SetFrom("blah#blah.com");
$mail->AddAddress("blah#blah.com");
$mail->Subject = "Blah"
$mail->MsgHTML('<html><body><img src="logo.jpg">Hello</body></html>');
$mail->AddAttachment("logo.jpg");
$mail->Send();
Using AddAttachment PHP Mailer will check your HTML for a reference to that file and automatically embed it for you.
buddy, its really simple. Write html code as if you write in developing a web page. Give the complete url to the image in the src attribute.
Dont forget to user the function eregi_replace() on body html
$html_message = eregi_replace("[\]",'',$body_html_string);
engoy !!!!! ;)

Categories