send mail with attachment

Submitted by remo - 2 years ago

Sometimes you want to attach a file to an e-mail. In concrete5 this is surprisingly difficult, but if you have a the proper recipe you shouldn't have a long time to get it working, check below!

<?php

$mh = Loader::helper('mail');

// create a file in mails/mail_template.php the usual way
$mh->load('mail_template');
 
// we need to get access to the underlying zend mail object
$zendMailerObject = $mh->getMailerObject();
$zendMailer = $zendMailerObject['mail'];
$zendMailer->setType(Zend_Mime::MULTIPART_RELATED);
 
$fileContent = file_get_contents($fileName);
$attachment = new Zend_Mime_Part($fileContent);
$attachment->filename  = basename($fileName);
$attachment->type = 'application/pdf';
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->disposition = Zend_Mime::DISPOSITION_INLINE;
$zendMailer->addAttachment($attachment);
 
$zendMailer->setFrom(EMAIL_DEFAULT_FROM_ADDRESS, EMAIL_DEFAULT_FROM_NAME);
$zendMailer->addTo('to@domain.told');
$zendMailer->setSubject($mh->getSubject());
$zendMailer->setBodyText($mh->getBody());
$zendMailer->setBodyHtml($mh->getBodyHTML());
 
$zendMailer->send($zendMailerObject['transport']);
Stats Categories Tags
comments powered by Disqus