Images from HTML email not displayed - php

I have an HTML mail sender function (code given below). It works, but when I include an image in the email HTML, the receiving inbox sometimes doesn't display it. I've tested and tested, and the rule is simple: Yahoo display the images, Gmail and Outlook don't.
Before you say anything, this is not an issue with the receiving Gmail/Outlook user not allowing images to be displayed. I've allowed images in both, and they both display a blank space (the size of the image) where the image is supposed to be (or ALT text if I set it). It's as if they think the image URL is wrong, but it can't be wrong because Yahoo loads it correctly. Also, I've viewed the message source in Outlook and the image source is correct there.
Here is the mail-sending code, pretty straight-forward:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=$charset\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: $from_address\n";
$headers .= "Reply-To: $from_address\n";
$headers .= "Return-Path: $from_address\n";
ini_set("sendmail_from",$CONST_MAIL);
mail($to_address,$subject,$message,$headers);
And $message might be something like the following:
<img alt="" src="http://www.domain.com/correctimagepath/img.jpg" id="newsletter_01" style="border: 0px none;" height="470" width="670">
<p>Hello world!</p>
Any ideas what might be wrong here?

Related

Send mail with embeded image without a library in php?

I'm sending mail with the mail() function of php. I embed image using base64 encoding inside the html code, but in gmail for example it doesn't show.
I read that a best way to do that is to use cid in the html code and attaching the image but I find only codes using phpmailer which I would like to avoid but the question is, is it possible with the php mail function or is there some simple classes to simplify the process ?
$message = $html_mail;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'To: '. $to . "\r\n";
$headers .= 'From: My website <'.$admin_mail.'>' . "\r\n";
$response = mail($to, $subject, $message, $headers);
Instead to using base64_encode image you should use html image tag with src path of your server, so it will download from your server.
<img src="http://www.yourserver.com/myimages/image1.jpg">
As your mail is having text/html header image will automatically parse via email client.
If you look at the "Original Source" in Gmail, you may find that gmail is editing the html to remove the image. idk if it does that or not though. I would try copying the html from the email in gmail and putting it in an html file and see if it loads in a browser correctly. Then you'll know for sure if gmail is messing it up or if it's a bug in another place.
FYI, sending html emails that work in all email clients can be a huge PAIN. The best guide I know of is this - https://www.sitepoint.com/how-to-code-html-email-newsletters/ . SitePoint also sells a book about it that is the gold standard IMO.
Good luck!

Add Picture In Email Body

i am not too good with PHP and programming but still i am trying to learn it in every possible way. I need help from experts. My code to send email is as below. i am able to send email, but the problem is, picture is not displaying in email body and it's showing a cross sign. any possible idea and suggestion is appreciated.
<?php
function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "CC: abc#gmail.com\r\n , xyz#gmail.com\r\n";
$headers .= "BCC: \r\n";
//set content type to HTML
$headers .= "Content-type: text/html\r\n";
if ( mail($to,$subject,$message,$headers) )
{
header("location:contact-us.php?msg=msgsent");
}
else
{
header("location:contact-us.php?msg=notsent");
}
}
//Function ends here
$from_email="";
$to_email="LMN#gmail.com";
$message.="<html><body background='images/bg11.jpg'>";
$message.="<p style=' color: #006789;'>Dear Team,</p> ";
$message.= "<p style=' color: #006789;'>You have received a feedback from $name & below are the details!!! </p>";
$message.= "<img src=images/c22.jpg>";
$message.="</body></html> ";
send_email(from_email,$to_email,$subject,$message);
?>
You are using a relative path:
<img src=images/c22.jpg>
When you open your mail, that path will mean nothing in a mail client and it is highly unlikely to exist on a webmail client. And if it does, it is not your image...
If you have that image stored on a web-server, you should use the absolute path to that image:
<img src="http://www.your-server.com/images/c22.jpg">
You're using a relative link, "images/c22.jpg", but you're sending this html in an email - there's no images/ directory relative to it to link to. If you wish to link to an image on the web, you can include a full url (http://someplace.com/images/bg11.jpg">). By default, those will usually not be displayed either - most email readers will filter them so as to not let you cause it to load remote content (tipping off that the email was opened, attempting funny business with malformated images, etc).
You can also attach an image to the email by setting the type to multipart/mixed and defining a boundary. Then send an image, defining a content-id in its headers, then your html linking to the content id as "cid:id-you-defined". Looks like this once in the email:
Content-Type: multipart/mixed; boundary="NOTUSEDINTEXT"
This text only shows if the reader can't deal with MIME
--NOTUSEDINTEXT
Content-Id: id_for_later
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU
FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo
KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAABAAEDASIA
AhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAj/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEB
AAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AJUAB//Z
--NOTUSEDINTEXT
Content-Type: text/html
<html><h1>Hi</h1><p>Here's an image <img src="cid:id_for_later"></html>
--NOTUSEDINTEXT--
.. but as you see it's slightly involved - might want to see if there's a php library that'll implement that (the mime headers and types) rather than pound them in yourself. But if you feel like dealing with it, inserting them yourself is ok too. And.. here too, some clients will filter them for safety unless you've clicked "Always show images" or something.
You have to upload your image on web server and give the absolute path to that image e.g:
<img src="http://www.yourwbesite.com/images/c22.jpg" alt="logo" />

using PHP to return an image in a base64 encoded email

I'm working on an email send and tracking application, but I'm running into a problem that I just can't figure out. I want to us a tracking image in my emails but I'm getting some strange behavior. Currently, I'm sending out emails encoded in base64. In the body of the HTML message I'm using an HTML image tag with a link to a PHP file on my server. That file sends back an image using the following PHP code:
header("Content-Type: image/png");
readfile("full-server-path-to-image.png");
This is where it gets strange. If I view the message in the Outlook email client the image will not display (just shows red "X" image icon). Other normally linked images will display just fine though. If I view the same message in Gmail I am able to see all of the images. It's really weird that the image serves up just fine in some email clients but not all of them.
I noticed this problem started when I changed to encoding my messages in base64. Before I started encoding my messages I was able to view all images in any email client. My gut is telling me this has something to do with the content-encoding-type, but I have no idea how to fix it. Any help would be appreciated!
Here is the PHP code I'm using to send out messages:
$myHTMLmessage = chunk_split(base64_encode('
<html><head></head><body>
<img src="https://www.my-web-site.com/openTrack.php?t={trackerid}"></a>
</body></html>'));
$boundary = uniqid('np');
$to = "somebody-else#another-web-site.com"
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: somebody#my-web-site.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain;charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
//Plain text body
$message .= "some plain text";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html;charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n\r\n";
//Html body
$message .= $myHTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
mail($to,$subject,$message,$headers);
Just in case anyone runs into something like this I figured out it all boils down to how email clients encode urls. In my original code I am using a url with 2 parameters inside my image src attribute. Like this:
http://www.my-web-site.com/page.php?pram1=something&pram2=something
When Outlook processes this url it get changed to:
http://www.my-web-site.com/page.php?pram1=something&pram2=something
Other email clients are smart enough to leave the & symbol the way it is without converting it to &
In my live code I needed to $_GET both url parameters and process them. If my script couldn't process both parameters then it wouldn't even get to the line where it would send back an image. The best workaround I could come up with was adding this code in my page.php file:
$pram1 = $_GET['pram1'];
$pram2 = $_GET['pram2'];
if(!isset($pram2)){
$pram2 = $_GET['amp;pram2'];
}
// now regardless of the email client both parameters contain a value to process
if((isset($pram1)) && (isset($pram2))){
header("Content-Type: image/png");
readfile("full-server-path-to-image.png");
}
I know is sounds pretty simple, but I had been scratching my head over this for way too long. Originally, I was convinced it was something to do with how the message was encoded before it was sent out. Never thought it would be Outlook changing my code. On second though Microsoft never likes to make things easy.....never gets old...lol

Using mail() to send an attachment AND text/html in an email in PHP4

To make life difficult, the client I'm working for is using a really large yet old system which runs on PHP4.0 and they're not wanting any additional libraries added.
I'm trying to send out an email through PHP with both an attachment and accompanying text/html content but I'm unable to send both in one email.
This sends the attachment:
$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$output = $attachment;
mail($emailTo, $emailSubject, $output, $headers);
This sends text/html:
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n";
$output = $emailBody; // $emailBody contains the HTML code.
This sends an attachment containing the text/html along with the attachment contents:
$headers = "Content-Type: text/html; charset='iso-8859-1'\r\n".$emailBody."\r\n";
$headers = "Content-Type: text/csv;\r\n name=\"result.csv\"\r\n Content-Transfer-Encoding: base64\r\n Content-Disposition: attachment\r\n boundary=\"PHP-mixed-".$random_hash."\"";
$output = $attachment;
What I need to know is how can I send an email with text/html in the email body and also add an attachment to it. I'm sure I'm missing something really simple here!
Thanks in advance.
Okay, I've finally cracked it! For reference:
$emailBody .= "<html><body>Blah</body></html>";
$emailSubject = "Subject";
$emailCSV = "\"Col1\", \"Col2\"\r\n"; // etc.
$attachment = $emailCSV;
$boundary = md5(time());
$header = "From: Name <address#address.com>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"" . $boundary . "\"\r\n";
$output = "--".$boundary."\r\n";
$output .= "Content-Type: text/csv; name=\"result.csv\";\r\n";
$output .= "Content-Disposition: attachment;\r\n\r\n";
$output .= $attachment."\r\n\r\n";
$output .= "--".$boundary."\r\n";
$output .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$output .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$output .= $emailBody."\r\n\r\n";
$output .= "--".$boundary."--\r\n\r\n";
mail($emailTo, $emailSubject, $output, $header);
Trying to send attachments using the PHP mail() function is an exercise in frustration; put simply, it's just not worth the effort. I would never ever suggest even attempting it, even in current PHP versions. I would always use a library like phpMailer.
I know you said you aren't allowed to use a third party library, but in this case, you would be crazy not to use one. We're talking about the difference between one day of work and a year; it's that big a deal. The PHP mail() function is incredibly difficult to work with, and trying to write code with it to send attachments from scratch will leave you with brittle, bug-ridden code that takes forever to get working reliably in all cases. This is why libraries like phpMailer exist.
So I suggest that regardless of what they want you to do, you should download phpMailer -- the PHP4 version is still available -- and see if it works for you. (The download page even still has the really old versions, so you should be able to go far enough back in time to find one that works with PHP 4.0. It shouldn't take you any time at all to get a simple demo up and running with it. Demonstrate that it works; that may well soften their stance against third party libraries.
(If it doesn't soften their stance, then there's not much hope of this project ever being completed in a sane amount of time. In that case, the best you can do is add a few extra zeros to your quote for the project price and grit your teeth)

PHP send plain text mail and force fixed-width charset

I want to force the reader's email client to display my php-generated plain text emails with a fixed width charset.
Thunderbird displays the email with a fixed width charset. However, Gmail and Outlook (and probably more clients) do not.
Is this a config setting with my mail server or something I'm doing wrong?
Any help would be greatly appreciated.
These are the headers I'm currently using:
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/plain; charset=ISO-8859-1; format=flowed\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";
$header .= "X-Mailer: PHP" . phpversion() ."\r\n";
$header .= "From: ihateoutlook#email.com\r\n";
$header .= "Reply-To: ihateoutlook#email.com\r\n";
You cannot force the plain text message to use any specific font. The best you can do is send HTML email using the monospace font family.

Categories