-
Inline image in e-mail
Hello,
I hope I can ask this questions here. I've looked and tried for some days now, but can't achieve quite what I want.
The problem is this:
For a few years now, I've been using a form on my website, which after submitting gets send to both the visitor and myself. But now, with updating this website, I got some errormessages/notices that I've been able to solve luckily. But at the same time, I thought it would be great to put a logo in the mail that gets send to the visitor. Easier said than done...
The website, www.cj-design.com and forum where this script ("Web2Mail") is from, seem to be abandoned. No reaction to my emails.
Searching the internet I found I have to use a 'Content-ID' for this, but do I set this myself, or do I have to let this ID rendered? Do I need just the name of the image, or the path to it as well?
And do I need the full base64-code, or is that not necessary when using 'Content-ID'?
(Off course I could just use the url to the image but that gets blocked by most email-clients because of spam-risks.)
The code I have so far (translated):
PHP Code:
function sendHTMLEmailAuto($titel, $name, $email, $subject, $message){
include("web2mail_config.php");
$recipient = "$name <$email>";
$rand = md5(time());
$mime_boundary = $rand;
$sub = "$auto_subject to $name";
$text_version = "Dear $name, thank you for contacting us.\n\nConcerning $subject we received the following message:\n\n $message\n\nWe'll contact you as soon as possible through: $email.\n\nKind regards, \n\n$yourname\n\n\n.";
$html_version = "<html>\n<head>\n<style>\nA:link {text-decoration: none; color: $alink_color;}\nA:visited {text-decoration: none; color: $vlink_color;}\nA:hover {font-style: normal; color: $hlink_color; text-decoration: underline;}\nBODY{font-family: $html_font; font-size: $html_font_size; color: #000066;}TD {font-family: $html_font; color: #000066; font-size: $html_font_size;}\n</style>\n</head>\n<body>\n\nDear <B>$name</B>, thank you for contacting us.\n<br>Concerning <B>$subject</B> we received the following message:\n\n<br><br>$message\n<br><br>We'll contact you as soon as possible through: $email. \n<br><br>\nKind regards, <br><br>\n$yourname $surname.<br><br>\n\n<a href=\"http://www.domain.com\">Companyname.com</a>\n<br><br><a href=\"http://www.domain.com\"><img src=\"cid:imagename\" alt=\"Companyname.com\" width=\"90\" height=\"90\"></a><br>\n<br>\n<br>\n</div>";
$headers = "Van: $your_website <$admin_email>\r\n";
$headers .= "Reply-to: $admin_email\r\n";
$headers .= "Return-Path: $admin_email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; charset=\"iso-8859-1\"; boundary=\"$mime_boundary\";\r\n"; // added ; before boundary
$headers .= "This is a multi-part message in MIME format.\r\n";
$msg = isset($mime_boundary) ? $mime_boundary : '';
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; // deleted boundary
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $text_version."\r\n\r\n";
$msg .= "--$mime_boundary\r\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $html_version."\r\n\r\n";
$msg .= "--$mime_boundary--\r\n\r\n\r\n";
$msg .= "Content-Type: image/jpg; name=\"imagename.jpg\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n\r\n";
$msg .= "Content-ID: <imagename> ";
$msg .= "Content-Disposition: inline; filename=\"imagename.jpg\"\r\n";
$msg = stripslashes($msg);
mail ($recipient, $sub, $msg, $headers);
}
Please, can anyone give me some clues what to adjust so this will work correctly and the logo will be visible in most email-clients? Thanks in advance.
-
Do you have the web2mail_config.php file? That appears to be essential as it's included in the function and two or more variables appear to be undefined, apparently they are defined in the config file. If you don't have that file, I would assume that is the problem.
-
Hello JScheuer1,
Yes, off course: I've been using this script for a few years. ;-)
The code:
PHP Code:
<?
$yourname = "Lockerbie";
$surname = "Familyname";
$your_website = "Companyname";
$your_webaddress = "http://www.domain.com";
$admin_email = "info@domain.com";
$show_ip = "no";
$auto_responder = "yes";
$auto_subject = "Automatic reply";
$error_messages = "yes";
$multipart_emails = "yes";
$html_font = "Verdana";
$html_font_size = "10pt";
$alink_color = "#000066";
$vlink_color = "#000066";
$hlink_color = "#CC0000";
$default_thank_msg = "no";
$thankyou_message_url = "http://www.domain.com/thanks.html";
?>
-
The original files.
Form.php:
PHP Code:
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table width="100%">
<tr><td align="left" width="15%">Your Name:</td>
<td align="left" width="80%"><input type="text" name="name" size="30" value="<? echo $_POST['name']; ?>"></td></tr>
<tr><td align="left" width="15%">Your E-mail:</td>
<td align="left" width="80%"><input type="text" name="email" size="30" value="<? echo $_POST['email']; ?>"></td></tr>
<tr><td align="left" width="15%">Subject:</td>
<td align="left" width="80%"><input type="text" name="subject" size="30" value="<? echo $_POST['subject']; ?>"></td></tr>
<tr><td align="left" width="15%">Message:</td>
<td align="left" width="80%">
<p align="left"><textarea cols="63" rows="10" name="message"><? echo $_POST['message']; ?></textarea></p>
<p align="left"><input type="submit" name="submit" value="Send Mail">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
mail_functions.php:
PHP Code:
<?
function sendHTMLEmail($name, $email, $message, $subject, $recipient, $ip, $date, $time){
include("web2mail_config.php");
$recipient = "$yourname <$admin_email>";
$rand = md5(time());
$mime_boundary = "CJ Web2Mail V3.0_". $rand;
$text_version = "Submitted: $date - $time\nFrom: $name - $email\n\n\n$message\n\nIP: $ip\n\n\nPowered by CJ Web2Mail V3.0 © http://www.cj-design.com";
$html_version="<html>\n<head>\n<style>\nA:link {text-decoration: none; color: $alink_color;}\nA:visited {text-decoration: none; color: $vlink_color;}\nA:hover {font-style: normal; color: $hlink_color; text-decoration: underline;}\nBODY{font-family: $html_font; font-size: $html_font_size;}TD {font-family: $html_font; color: #000000; font-size: $html_font_size;}\n</style>\n</head>\n<body>\n\n<table border=\"0\" cellpadding=\"6\" cellspacing=\"0\" width=\"100%\">\n<tr>\n <td nowrap width=\"1%\">\n<b>Submitted</b>\n</td>\n<td width=\"99%\">$date - $time</td>\n</tr>\n<tr>\n<td nowrap width=\"1%\">\n<b>From</b></td>\n<td width=\"99%\">$name - <A HREF=\"mailto:$email\">$email</A></td>\n</tr>\n<tr>\n<td nowrap width=\"1%\" valign=\"top\">\n<b>Message</b></td>\n<td width=\"99%\">".nl2br($message)."</td>\n</tr>\n<tr>\n<td nowrap width=\"1%\"><b><small>IP Address</small></b></td>\n<td width=\"99%\"><small>$ip</small></td>\n</tr>\n</table><br>\n<br>\n<div align=\"right\"><i>Powered by <A HREF=\"http://www.cj-design.com\">CJ Web2Mail V3.0</A></i></div></body></html>";
$headers = "From: $name <$email>\r\n";
$headers .= "Reply-to: $email\r\n";
$headers .= "Return-Path: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; charset=\"iso-8859-1\"; boundary=\"$mime_boundary\";\r\n\r\n"; // added ; before boundary
$headers .= "This is a multi-part message in MIME format.\r\n\r\n";
$msg .= "--$mime_boundary\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; // deleted boundary
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $text_version."\r\n\r\n";
$msg .= "--$mime_boundary\r\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $html_version."\r\n\r\n";
$msg .= "--$mime_boundary--\r\n\r\n\r\n";
return mail($recipient, $subject, $msg, $headers);
}
function sendHTMLEmailAuto($name, $email, $subject){
include("web2mail_config.php");
$recipient = "$name <$email>";
$rand = md5(time());
$mime_boundary = "CJ Web2Mail V3.0_". $rand;
$sub = "$auto_subject to $name";
$text_version = "$name, thank you for emailing $your_website.\n\nWe will get back to you soon concerning '$subject'.\n\nKind Regards\n$yourname\n\n\nNote: This is an automated response, please do not reply to this\n\n\nPowered by CJ Web2Mail V3.0 © http://www.cj-design.com";
$html_version = "<html>\n<head>\n<style>\nA:link {text-decoration: none; color: $alink_color;}\nA:visited {text-decoration: none; color: $vlink_color;}\nA:hover {font-style: normal; color: $hlink_color; text-decoration: underline;}\nBODY{font-family: $html_font; font-size: $html_font_size;}TD {font-family: $html_font; color: #000000; font-size: $html_font_size;}\n</style>\n</head>\n<body>\n\n<B>$name</B>, thank you for emailing $your_website.\n\n<br><br>We will get back to you soon concerning '<I><B>$subject</B></I>'.\n<br><br>\nKind Regards<br>\n$yourname<br>\n<br>\n<br>\n<small>Note: This is an automated response, please do not reply to this</small><br>\n<br><div align=\"right\"><i>Powered by <A HREF=\"http://www.cj-design.com\">CJ Web2Mail V3.0</A></i></div>";
$headers = "From: $your_website <$admin_email>\r\n";
$headers .= "Reply-to: $admin_email\r\n";
$headers .= "Return-Path: $admin_email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; charset=\"iso-8859-1\"; boundary=\"$mime_boundary\";\r\n\r\n"; // added ; before boundary
$headers .= "This is a multi-part message in MIME format.\r\n\r\n";
$msg .= "--$mime_boundary\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; // deleted boundary
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $text_version."\r\n\r\n";
$msg .= "--$mime_boundary\r\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $html_version."\r\n\r\n";
$msg .= "--$mime_boundary--\r\n\r\n\r\n";
$msg = stripslashes($msg);
mail ($recipient, $sub, $msg, $headers);
}
function sendTextEmailAuto($name, $email, $subject){
include("web2mail_config.php");
$recipient = "$name <$email>";
$sub = "$auto_subject to $name";
$msg = "$name, thank you for emailing $your_website.\n\nWe will get back to you soon concerning '$subject'.\n\nKind Regards\n$yourname\n\n\nNote: This is an automated response, please do not reply to this\n\n\nPowered by CJ Web2Mail V3.0 © http://www.cj-design.com";
$headers = "From: $your_website <$admin_email>\r\nReply-To: $admin_email\r\n";
$msg = stripslashes($msg);
mail ($recipient, $sub, $msg, $headers);
}
?>
thankyou.php:
PHP Code:
<?
if($sent){
?>
Thank you <? echo $name; ?>, we will contact you shortly.<br><br>
Your message:<br><br>
<table border="0" cellpadding="10" cellspacing="0" width="450">
<tr>
<td width="440"><small><? echo nl2br($message); ?></small></td>
</tr>
</table>
<br><br>
<?
if($show_ip == "yes"){
echo "<small>We have captured your IP (<I>$ip</I>) for security reasons</small>";
}
}
else{
print "$name, we could not send your message at this time, please try again later";
}
?>
web2mail.php:
PHP Code:
<!-- You can insert a stylesheet here -->
<?
include "web2mail_config.php";
$path = __FILE__;
$path = preg_replace( "'\\\web2mail\.php'", "", $path);
$path = preg_replace( "'/web2mail\.php'", "", $path);
$send = false;
if(isset($_POST['submit'])){
$date = date("l d F Y");
$time = date("g:i:s a");
$ermsg = "";
$send = true;
$ip = $_SERVER['REMOTE_ADDR'];
$name = cleanUp($_POST['name']);
$email = cleanUp($_POST['email']);
$message = $_POST['message'];
$subject = cleanUp($_POST['subject']);
$emailvalid = is_valid_email($email);
if (!isset($name) || $name == ""){
$emsg = "<li>Please enter your name"; $send = false;
}
if (!isset($email) || $email == "" || $emailvalid == 1){
$emsg .= "<li>Your email address is missing or incorrect"; $send = false;
}
if (!isset($subject) || $subject == ""){
$emsg .= "<li>Please enter a subject"; $send = false;
}
if (!isset($message) || $message == ""){
$emsg .= "<li>Your message is blank"; $send = false;
}
if($send){
$sent = false;
include("mail_functions.php");
$message = strip_tags($message);
$message = stripslashes($message);
$subject = stripslashes($subject);
if($multipart_emails == "yes"){
if(sendHTMLEmail($name, $email, $message, $subject, $recipient, $ip, $date, $time)){
$sent = true;
}
}
else{
$recipient = "$yourname <$admin_email>";
$headers = "From: $name <$email>\r\nReply-To: $email\r\n";
$msg = "Submitted: $date - $time\nFrom: $name - $email\n\n\n$message\n\nIP: $ip\n\n\nPowered by CJ Web2Mail V3.0 © http://www.cj-design.com";
if(mail ($recipient, $subject, $msg, $headers)){
$sent = true;
}
}
if($sent){
if($auto_responder == "yes"){
if($multipart_emails == "yes"){
sendHTMLEmailAuto($name, $email, $subject);
}
else{
sendTextEmailAuto($name, $email, $subject);
}
}
if($default_thank_msg == "yes"){
include($path."/thankyou.php");
}
else{
header("Location: $thankyou_message_url");
}
}
else{
include($path."/thankyou.php");
}
}
}
if(!$send){
if(strlen($emsg) > 0 && $error_messages == "yes"){
echo "Your submission had the following errors:<br><br><FONT COLOR=\"red\">$emsg</FONT><br><br>";
}
include($path."/form.php");
}
function cleanUp($field)
{
//Remove line feeds
$ret = str_replace("\r", "", $field);
$ret = str_replace("\n", "", $ret);
// Remove injected headers
$find = array("/bcc\:/i",
"/Content\-Type\:/i",
"/Mime\-Type\:/i",
"/cc\:/i",
"/to\:/i");
$ret = preg_replace($find, "", $ret);
return $ret;
}
function is_valid_email($sender_mail) {
if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)", $sender_mail)) {
return 0;
}
else{
return 1;
}
}
?>
web2mail_config.php:
PHP Code:
<?
$yourname = "Your Name";
$your_website = "Your Site Name";
$your_webaddress = "http://www.yoursite.com";
$admin_email = "yourname@youremail.com";
$show_ip = "yes";
$auto_responder = "yes";
$auto_subject = "Auto Responder";
$error_messages = "yes";
$multipart_emails = "yes";
$html_font = "verdana";
$html_font_size = "10pt";
$alink_color = "#11116E";
$vlink_color = "#11116E";
$hlink_color = "#000000";
$default_thank_msg = "yes";
$thankyou_message_url = "";
?>
-
Right, I downloaded that package later to see what was in it. So what ever made you think you could throw an image in there?
Anyways, in the absence of any other help or documentation, I think you've pretty much answered your own question. Though that might not be what you meant exactly. "the full base64-code" - that should work if used directly in the image tag (here):
Code:
<img src=\"cid:imagename\"
I'd try replacing the red part with it (can be a variable passed in from elsewhere, but not a script that returns the image).
Here's a handy online utility for converting the image if you need it, after conversion you can even scroll down to get the actual img tag, from which you can extract the exact src attribute required:
http://base64.wutils.com/encoding-on...age-to-base64/
I was surprised how (relatively speaking) little text was required to encode a fairly good sized image. I used an optimized image, which might be a good idea, nevertheless it wasn't an overwhelming amount of text for a good sized photo. For a small logo, it should be quite manageable, though setting it separately as a variable would be good. I think there are PHP GD scripts for returning the encoded data, those would work as well.
-
I guess there have to be another adjustment, because this still isn't working. With the base64-code instead of the 'cid:imagename'-part doesn't show an image in Outlook or in Gmail...
-
We might need more help on this. That said - What exact code are you using? Did you test it on a plain html page inside an img tag as its src attribute to verify the code is correct? Does it have the proper data:image/image type;base64, prefix? where image type is for example png or jpeg?
Anyways, what exact code did you put in there?
-
When I use:
PHP Code:
function sendHTMLEmailAuto($titel, $name, $email, $subject, $message){
include("web2mail_config.php");
$recipient = "$name <$email>";
$rand = md5(time());
$mime_boundary = $rand;
$sub = "$auto_subject to $name";
$text_version = "Dear $name, thank you for contacting us.\n\nConcerning $subject we received the following message:\n\n $message\n\nWe'll contact you as soon as possible through: $email.\n\nKind regards, \n\n$yourname\n\n\n.";
$html_version = "<html>\n<head>\n<style>\nA:link {text-decoration: none; color: $alink_color;}\nA:visited {text-decoration: none; color: $vlink_color;}\nA:hover {font-style: normal; color: $hlink_color; text-decoration: underline;}\nBODY{font-family: $html_font; font-size: $html_font_size; color: #000066;}TD {font-family: $html_font; color: #000066; font-size: $html_font_size;}\n</style>\n</head>\n<body>\n\nDear <B>$name</B>, thank you for contacting us.\n<br>Concerning <B>$subject</B> we received the following message:\n\n<br><br>$message\n<br><br>We'll contact you as soon as possible through: $email. \n<br><br>\nKind regards, <br><br>\n$yourname $surname.<br><br>\n\n<a href=\"http://www.domain.com\">Companyname.com</a>\n<br><br><a href=\"http://www.domain.com\"><img alt=\"imagename.jpg\" src=\"data:image/jpeg;base64,/1234567890thetotalcodeofthebase64here\" width=\"90\" height=\"90\"></a><br>\n<br>\n<br>\n</div>";
$headers = "Van: $your_website <$admin_email>\r\n";
$headers .= "Reply-to: $admin_email\r\n";
$headers .= "Return-Path: $admin_email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; charset=\"iso-8859-1\"; boundary=\"$mime_boundary\";\r\n"; // added ; before boundary
$headers .= "This is a multi-part message in MIME format.\r\n";
$msg = isset($mime_boundary) ? $mime_boundary : '';
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; // deleted boundary
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $text_version."\r\n\r\n";
$msg .= "--$mime_boundary\r\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$msg .= $html_version."\r\n\r\n";
$msg .= "--$mime_boundary--\r\n\r\n\r\n";
$msg .= "Content-Type: image/jpg; name=\"imagename.jpg\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n\r\n";
$msg .= "Content-ID: <imagename> ";
$msg .= "Content-Disposition: inline; filename=\"imagename.jpg\"\r\n";
$msg = stripslashes($msg);
mail ($recipient, $sub, $msg, $headers);
}
The results are:
- Outlook 2010 doesn't show a picture, only a red cross,
- Gmail doesn't show a picture, only the alt-text,
- The emailprogram on Android-devices shows the logo directly, ánd there is this button 'show all images' (but this is the only one, already visible).
-
I'm looking on Google and all I see about using images in email generated via PHP is to just to include the src to the image (full url on the server) in the image tag. Or, and I'm thinking this is what you're trying to do with your code, it's suggested that PHPMailer can include the image directly in the mail. It's available for free with documentation here:
https://github.com/Synchro/PHPMailer
I would caution you not to get your hopes up too much though. There are email programs and there are email programs. For instance, using gmail - I see all images without having to give permission. When I signed up for this integrated web service it was explained that, because gmail is a highly secure system you can see all images immediately without fear of security violations. Whereas my previous program which was simply one where I downloaded mail from the server would not show any images under any circumstances unless I told it to for either a particular message, or always for messages from a particular sender. I'm pretty sure Outlook (or at least some versions of it) is like that. Many older programs that are on your computer and that simply download messages from the server are like that, or worse - simply do not show any images. It may be a configuration option, but many will stringently warn against enabling images.
-
I also found a much simpler looking package at:
http://www.phpclasses.org/package/36...tachments.html
Also free, but you have to sign up for it, which I did using one of my existing accounts. The code looks really easy to use.
Anyways, looking at these packages, I see why it's so complicated. I would recommend using one of them or another one with support, rather than this old package you currently have that's not working.
Yet another popular package:
http://swiftmailer.org