![]() |
|
Snippets |
|
Embedding images in emails as described in the symfony book has one drawback: it breaks the MVC paradigm by setting properties of the view (embedding the image) in the controller.
This is probably more obvious when embedding images such as company logos that are not part of a query result, but instead are 'static' visual enhancements of the view.
Besides being not good practice, this results in more code and potential mistakes when using several images, as you need to keep track of cid's both in the action and in the template.
This helper lets you treat images in emails similar to images in regular HTML views. There are no calls to $mail->addEmbeddedImage() needed in the action, embedding is done exclusively in the view, and you can forget about cid's.
Update: In the original version, an image used twice in the email would also be embedded twice, increasing email size. This bug is fixed.
Images are embedded like this:
<?php echo embedded_image($this, '/myImageDir/myImage.jpg' [, $altText [, $encoding [, $mimeType]]]); ?>
The parameters are:
$this: a reference to the current sfMailView
filename: the path to the image to be embedded
$altText: alternative name for attachment, default is ''
$encoding: encoding, default is base64
$mimeType: mime type, when omitted, mime type is determined using the file extension of the image
function embedded_image( $mailView, $fileName, $altText = '', $encoding = 'base64', $mimeType = null ) { // get list of files already embedded $embeddedList = $mailView->getParameter('embedList', array(), 'email_helper'); if (array_key_exists($fileName, $embeddedList)) { // if image was already embedded, use its old cid $embedString = $embeddedList[$fileName]; } else { // find mime type if ($mimeType == null) { $mimeMap = array( 'gif' => 'image/gif', 'jpg' => 'image/jpeg', 'png' => 'image/png' ); $ext = strtolower(array_pop(explode('.', $fileName))); if (array_key_exists($ext, $mimeMap)) { $mimeType = $mimeMap[$ext]; } else { $mimeType = 'image/jpeg'; } } // increment embedded image count $imgID = $mailView->getParameter('embedIndex', 0, 'email_helper') + 1; $mailView->setParameter('embedIndex', $imgID, 'email_helper'); $embedString = 'embID_' . $imgID; // add image to mail $mailView->getAttribute('mail')->addEmbeddedImage( $fileName, $embedString, $altText, $encoding, $mimeType ); // remember embedded file $embeddedList[$fileName] = $embedString; $mailView->setParameter('embedList', $embeddedList, 'email_helper'); } return '<img src="cid:' . $embedString . '" />'; }
This is very simple and effective mail helper.
Original code is taken from Smarty's mailto plugin.
This helper scrambles your email addresses so those nasty web bots 'can not' harvest them.
<?php function encode_mail($Address, $Caption=null) { if ($Caption == null) $Caption = $Address; $string = 'document.write(\'<a href="mailto:'.$Address.'">'.$Caption.'</a>\');'; $js_encode = ''; for ($x=0; $x < strlen($string); $x++) $js_encode .= '%' . bin2hex($string[$x]); return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>'; } ?>
<?php use_helper('MailEncode') ?> My email: <?php echo encode_mail('info@example.com'); ?> or My email: <?php echo encode_mail('info@example.com', 'Damjan Malis'); ?>
Provide 4 functions which crypt e-mail address with javascript in order to not be captured by spammers robots.
antispam_email_tag($emailaddress, $content) : generate an e-mail link_tag (mailto) encrypted in javascript
antispam_js_encrypt($content) : encrypt in javascript some text
antispam_auto_link_email_addresses($text) : Turns all email addresses into clickable links. Email links are encrypted.
antispam_email_in_html($html) : Encrypt all e-mail link in HTML
[php]
function antispam_email_tag($emailaddress, $content=null, $options = array()) { $options = _parse_attributes($options); $options['href'] = 'mailto:'.$emailaddress; if($content===null) $content=$emailaddress; return antispam_js_encrypt(content_tag('a',$content,$options),rand(-8,5)); }
[php]
function antispam_js_encrypt($content,$chr_adjust=-1) { $encoded_content=''; for ($n = 0; $n < strlen($content); $n++) { $encoded_content .= dechex(ord(substr($content,$n,1))+ $chr_adjust) ; // + $chr_adjust } $rdm_function_name = ''; while(strlen($rdm_function_name)<8) { $tmp = rand ( 65, 122); if($tmp > 96 || $tmp < 91 ) $rdm_function_name .= chr($tmp); } $js_function='function '.$rdm_function_name."(e) { for (i = 0; i <= e.length; i+=2) { document.write(String.fromCharCode((parseInt((('0x') + e.substring(i,i+2)),16)) - (".$chr_adjust.")));}}"; return "<script>".$js_function."\n".$rdm_function_name."('" .$encoded_content. "');</script>"; }
[php]
function antispam_auto_link_email_addresses($text) { $found=true; $offset=0; while($found) { $found=preg_match('/[\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+\.[A-Za-z0-9\-]+/',$text,$matches, PREG_OFFSET_CAPTURE,$offset); if ($found) { $antispam_email_link=antispam_email_tag($matches[0][0]); $text=substr_replace ($text,$antispam_email_link,$matches[0][1],strlen($matches[0][0])); $offset=$matches[0][1]+strlen($antispam_email_link); } } return $text; }
[php]
function antispam_email_in_html($html) { $regex = '#<([aA])(\s)*(href|HREF)(\s)*=(\s)*[\"|\'](mailto:|MAILTO:)(.*?)[\"|\'](.*?)>(.*?)</\1>#is'; $found=true; $offset=0; while($found) { $found=preg_match($regex,$html,$matches, PREG_OFFSET_CAPTURE,$offset); if ($found) { $antispam_email_link=antispam_email_tag($matches[7][0],$matches[9][0]); $html=substr_replace ($html,$antispam_email_link,$matches[0][1],strlen($matches[0][0])); $offset=$matches[0][1]+strlen($antispam_email_link); } } return $html; }