I'm sending HTML mails with swiftmailer. It all works pretty great, my HTML mails are showing up just fine in all clients. I have quite the knowledge of building email templates. Sending them myself not so much...
The problem exists in gmail and hotmail. The image is not showing up. It shows just fine in Thunderbird for instance... gmail is leaving my image blank and adds it as an attachment (at the bottom of the mail, like i'm sending birthday pictures..).
Any ideas?
I've got the following code:
function deliverMail($subject, $data, $from, $to, $template){
if($_SERVER['SERVER_ADDR'] !== '::1'){
if (!is_array($to)){
$to = array($to);
}
$from = explode(",", $from);
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.xxx.nl', 25)
->setUsername('xxx')
->setPassword('xxx');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject);
$image = $message->embed(Swift_Image::fromPath($_SERVER['DOCUMENT_ROOT'].'/templates/emails/xxx.jpg'));
// open the file
$sourcefile = $_SERVER['DOCUMENT_ROOT'].'/templates/emails/'.$template.'.html.tpl';
$fh = fopen($sourcefile, 'r');
$htmltemplate = fread($fh, filesize($sourcefile));
fclose($fh);
// set the content
if($template == 'confirm'){$replaceOld = array("[*code*]", "[*imgsrc*]");}
if($template == 'notify'){$replaceOld = array("[*url*]", "[*imgsrc*]");}
$replaceNew = array($data, $image);
$body = str_replace($replaceOld, $replaceNew, $htmltemplate);
$message->setFrom(array($from[0] => $from[1]))
->setTo($to)
->setBody($body, 'text/html');
if (!$mailer->send($message, $failures)){
return "Failures:";
return print_r($failures);
}else{
return 'success';
}
}
}
Which will render the image in my template here:
<td id="head" height="80" width="640" colspan="3" valign="top" align="left">
<image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>
</td>
I have seen this behaviour in GMail as well and think this is just how GMail displays mail with embedded images.
See this link for more information:http://www.getelastic.com/email-design-for-gmail/
Did you know that Gmail disables
images in HTML emails by default, even
if your customer has added you to his
or her safe list?
Wow, finally settled this. "image" is not a valid HTML tag of course... after trying 3 different php libraries i found this..
<td id="head" height="80" width="640" colspan="3" valign="top" align="left">
<image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>
</td>
Related
I'm using PHPMailer to send the registration confirm email to my users with a full html file. The email shows perfectly except for the pictures are not loaded.
Here I attach my php code:
$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8';
$mail->setFrom($this->Settings->SUPPORT_EMAIL_ADDRESS);
$mail->AddAddress($email);
$mail->Subject = dblang('registration_confirm_email_subject');
if (file_exists(APPPATH . '/views/email/registration/confirm.txt.' . $this->lang->lang() . '.php')) {
$mail->AltBody = $this->load->view('_email/registration/confirm.txt.' . $this->lang->lang() . '.php',
$emailData, true);
}
$mail->isHTML(true);
$mail->Body = $this->load->view('_email/registration/confirm.html.' . $this->lang->lang() . '.php',
$emailData, true);
if($mail->send()) {
swal_success('registration_email_send_title', 'registration_email_send_text');
redirect(site_url('home'));
}
Here I put a part of the html code. I've tried with relative and absolute urlfor the pictures path:
<img src="../../../../../../bets/assets/email/welovesports.png" alt="We Love Sports" border="0" width="290" height="92"
style="display: block;border: none;outline: none;text-decoration: none;-ms-interpolation-mode: bicubic;">
Since you've tried absolute urls with no luck.
looking on your code reference ../../../../../../bets/assets/email/
I suggest copy that image to very root of public_html -> /public_html/welovesports.png
and then reference it as <img src="http://your-domain.test/welovesports.png">
because the no. of directories your code suggests, can make it difficult to reference the correct path
OR Alternatively
you can use http://imgur.com/upload and host your image with them.
and on the gallery page of that image copy its direct link to use it
(it will be like http://i.imgur.com/RDz0KBs.png)
eg: http://i.imgur.com/RDz0KBs.png
When a client receives a review on the web from a specific service, that service sends and e-mail that notifies of the new review. The content of the e-mail includes the review itself, and source information, the content comes as (e-mail header, e-mail body PART 1(plain text) and e-mail body PART 2 (HTML). I need to parse the HTML section of those e-mails and push them out to a new flatfile for PHP include on the clients testimonials page.
I am successfully able to connect to my mail service, and parse section 2 of the test e-mail i am using to test with. the problem is that when the output is viewed in the browser it's simply blank. But when I view source - all of the content inclusive of HTML code/structure/css is there. I'm at a loss as to why I see nothing (plaint text or HTML) on the front end of the browser but see everything in source view.
Here is my code:
$login="*email user name*";
$password="*email password*";
$connection = imap_open('{pop.secureserver.net:995/novalidate-cert/pop3/ssl}', $login, $password);
$count = imap_num_msg($connection); /* get number of messages on server */
$i = 46; /* message 46 is the message being used to test this */
$header = imap_header($connection, $i);
$body = imap_fetchbody($connection,$i,"2"); /* grab section 2 of e-mail (HTML) */
$prettydate = date("F jS Y", $header->udate); /* not necessary just part of testing response */
if (isset($header->from[0]->personal)) {
$personal = $header->from[0]->personal;
} else {
$personal = $header->from[0]->mailbox;
}
$email = "$personal <{$header->from[0]->mailbox}#{$header->from[0]->host}>";
echo "On $prettydate, $email said <hr>";
echo $body;
echo "<hr>";
imap_close($connection);
First 15 lines of "view source" after PHP response from within chrome.
On August 3rd 2014, New Review Notifications <pl-no-reply#reviews.com> said <hr><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=3D"http://www.w3=
.org/1999/xhtml"> <head> <meta http-equiv=3D"Content-Type" content=3D"text/=
html; charset=3Dutf-8" /> <title></title> <style type=3D"text/css"> .Extern=
alClass{display:block !important;} .yshortcuts, .yshortcuts a, .yshortcuts =
a:link, .yshortcuts a:visited, .yshortcuts a:hover, .yshortcuts a span{ col=
or:#008ec5; text-decoration:none !important; border-bottom:none !important;=
background:none !important; } body{margin:0;} p{margin:0 !important;} </st=
yle> </head> <body marginheight=3D"0" marginwidth=3D"0" leftmargin=3D"0" to=
pmargin=3D"0" bgcolor=3D"#ffffff"> <table width=3D"100%" cellpadding=3D"0" =
cellspacing=3D"0" bgcolor=3D"#ffffff"> <tr> <td height=3D"25" style=3D"back=
ground-color: #88939B" colspan=3D"3"></td> </tr> <tr> <td width=3D"50%" val=
ign=3D"top"> <table width=3D"100%" cellpadding=3D"0" cellspacing=3D"0" styl=
e=3D"height: 232px;"> <tr> <td style=3D"background-color: #88939B; height: =
232px; display: block"> </td> </tr> </table> </td> <td width=3D"632"> =
PS - can someone with higher rep add imap-fetchbody to tag list if applicable, it wont let me.
Two problems here: a. encoding, b. content display.
Encoding
Somewhere in header (or body) is a property encoding.
This tell's you how your mail content is encoded.
You can then use these pieces of information to revert the encoding.
It might be $body->encoding, instead of $header->encoding.
$body = imap_fetchbody($connection,$i,"2");
if ($header->encoding == 4) {
$body = quoted_printable_decode($body);
}
if ($header->encoding == 3) {
$body = base64_decode($body);
}
echo $body; // now body should have the correct encoding
Give this a try, too:
$body = imap_fetchbody($connection,$i, 1.2); <-- instead of 2
Content Display
As Marc B already pointed out, it's not possible to render a complete HTML page inside a HTML page, without an iframe. An iframe is the easiest way to display this.
But you have several options here, from tag-removal over body-extraction.
If you remove the "important" tags, you get the content.
preg_matching for <body>.*</body> should work, too.
$body = str_replace('<html>', '', $body);
$body = str_replace('<head>', '', $body);
$body = str_replace('<title>', '', $body);
$body = str_replace('<body>', '', $body);
$body = str_replace('</body>', '', $body);
$body = str_replace('</html>', '', $body);
I'm trying to send an email with an image in base64 on the body with PHP using the code below, but the image never appears... If I change to an URL it works, but it doesn't with the base64... I tested the base64 on a new page only with <img src=base64> and worked too... What am I missing??
<?php
// recipients
$to = $_POST['email'];
// subject
$subject = 'Test';
// message
$message = '
<html>
<head>
<title>Test</title>
</head>
<body>
<img src="'.$_POST['imageFromOtherPage'].'"/>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Here is my base64 image example: http://jsfiddle.net/28nP4/
I tried different things and the only way I found was uploading the image and getting the URL, I got that from this link: http://j-query.blogspot.in/2011/02/save-base64-encoded-canvas-image-to-png.html
It is very simple:
<?php
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
And this generates an URL, so, instead of using <img src="'.$_POST['imageFromOtherPage'].'"/>, I use the generated URL. Worked perfectly!
i have the same problem, and finaly i resolved it. It might be helpful for you:
You can use SwiftMailer, but you must to add new Image TextHeader 'Content-Location'.
Here is the code:
$message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail, 'Name')->setTo($toEmail)->setBcc($bccEmails);
/*get uniqueID from cid:uniqueID */
$imageID = explode(':', $message->embed(\Swift_Image::fromPath('pathToTheImage')->setContentType('image/png')))[1];
/*Add Content-Location to image header*/
/** #var \Swift_Image $image */
$image = $message->getChildren()[0];
$image->getHeaders()->addTextHeader('Content-Location', $imageID);
$message->setBody('here you will have some html with <img src=$imageID alt="Embed Image">', 'text/html');
$mailer->send($message);
function for get all images url :
function getImagesFromMsg($msg, $tmpFolderPath)
{
$arrSrc = array();
if (!empty($msg))
{
preg_match_all('/<img[^>]+>/i', stripcslashes($msg), $imgTags);
//All img tags
for ($i=0; $i < count($imgTags[0]); $i++)
{
preg_match('/src="([^"]+)/i', $imgTags[0][$i], $withSrc);
//Remove src
$withoutSrc = str_ireplace('src="', '', $withSrc[0]);
//data:image/png;base64,
if (strpos($withoutSrc, ";base64,"))
{
//data:image/png;base64,.....
list($type, $data) = explode(";base64,", $withoutSrc);
//data:image/png
list($part, $ext) = explode("/", $type);
//Paste in temp file
$withoutSrc = $tmpFolderPath."/".uniqid("temp_").".".$ext;
#file_put_contents($withoutSrc, base64_decode($data));
}
//Set to array
$arrSrc[] = $withoutSrc;
}
}
return $arrSrc;
}
Try to test if $_POST['imageFromOtherPage'] contains the base64 , probably the code is too long for a Post, you would need to use php://input .
<img src="'.$_POST['imageFromOtherPage'].'"/>
Your POST needs to start with data:image/png;base64, to form a base64 encoded data: URI.
But even if you fix this, it is well possible that the E-Mail client you're viewing the message in doesn't support the method at all. data:URIs are a relatively new phenomenon - at least in the chronology of E-Mail clients, most of which are still in the process of discovering that there's a technology called CSS. The tried and tested method for images in E-Mails is to embed the image as an inline attachment.
Here are some approaches that don't rely on a library: Make PHP send an email with an inline image attachment
However, using a library like Swiftmailer may take away a lot of pain. Here is the appropriate Swiftmailer documentation.
I have a big problem! I need to track what the users are doing on my site. As a way to resolve it I crated a sendmail function in order to send me an email every time a user clicks on a button. The code is this:
<div class="buy">
<a onclick="target='_blank'" href="<?php echo $this->product['from'];?>">
<?php
// The message
$message = "A new buy";
$link = "<?php echo $this->product['from'];?>";
// Send
mail('xxx#mail.com', '#buy PRODUCT', $message, $link);
?>
<img src="http://xxx.com/data/images/xxx.jpg" alt="Comprar" />
</a>
</div>
The message I receive is
"A new buy
**<?php echo $this->product['from'];?>**"
And it should look like:
"A new buy
http://www.xxxx.com"
Anyone can help me with this problem?
Ok, then try this:
$message = "A new buy ".PHP_EOL.PHP_EOL;//add two new lines for plaintext message
$message .= $this->product['from']; //add link to the end of message
// Send
mail('xxx#mail.com', '#buy PRODUCT', $message); //no need for fourth parameter
Read further:
http://php.net/manual/en/function.mail.php
And for easy e-mailing use phpmailer:
http://code.google.com/a/apache-extras.org/p/phpmailer/
Instead of:
$link = "<?php echo $this->product['from'];?>";
use
$link = $this->product['from'];
I have used this code from link below to send excel file as attachment.
http://www.hotscripts.com/forums/php/22626-php-form-excel-then-send-email-attachment.html
Here is the code I have used:
<?php
ob_start();
?>
<html>
<head><title></title>
</head>
<body>
<body>
<center>
<table>
<tr>
<td>Name:</td><td>My Name</td>
</tr>
<tr>
<td>Address:</td><td>My address</td>
</tr>
<tr>
<td>Gender:</td><td>Female</td>
</tr>
</table>
</body></html>
<?php
$FILE_CONTENTS = ob_get_contents();
ob_clean();
// include the class
include("../includes/classes/class.mailer.php");
$recipient = "myid#domain.com";
$from = "myid#domain.com";
// subject
$subject = "Subject of email";
// email message
$message = "
Here goes the message.
";
$myEmail = new EPDEV_Emailer($recipient, $from, $subject);
$myEmail->addText($message);
$myEmail->addFile("my.xls", "application/vnd.ms-excel", $FILE_CONTENTS);
$myEmail->send();
?>
I get excel file in mail but the file does not contain the lines as the normal excel file contains. But i need the file with lines as excel file contains. Can anyone suggest me what the problem might be.
Thanks in advance
I think that you should split your problem in two:
Do you have problems with reading other file types? Try to send a text file and a JPG and see if you have problems reading them as well.
Are you sure the Excel files are OK? Try to save them locally on server, then download by FTP and try to open.
I would guess that the problem is in generating the Excel files (2.), not with sending them by email (1.). But that's just a guess.