Unable to attach file from MandrillEmail::sendTemplate()
zanderwar opened this issue · 7 comments
Hey, I'm might be looking at this incorrectly but im trying to attach a file while using the sendTemplate function. (I don't need the GUI just the functionality)
$mailer = MandrillEmail::create();
$mailer->setTo($to);
$mailer->setRecipientMetadatas($to, []);
$mailer->setFrom('noreply@example.com.au');
$mailer->setSubject($subject);
$file = '/home/user/public_html/myfile.png';
$mailer->attachFile($file); // this doesn't work. It doesn't appear to call the encodeFileForEmail anywhere thats within `MandrillMailer`
$mailer->sendTemplate(
'application-approval',
$mergeVars
);
The email is sent successfully but no file is attached
Happy to PR this in, for my need I just added and extra param to sendTemplate
at the end called attachFiles
which passed it through to MandrillMailer::sendTemplate()
which then encodes the attachment in
you don't need to use the mailer to attach files. you use the email object with the attachFile method
http://api.silverstripe.org/3.6/source-class-Email.html#207-224
so you can simply use the framework default features for this
Sadly after several attempts of using the stock standard attachFile
prior to arriving here (and in the code example above), I had absolutely no luck and regardless of what I attempted to attach; it would not come through, nor never called encodeFileForEmail
attachFiles
is not actually overridden anywhere so I'm not sure how it creates the relevant structure that mandrill needs to send attachments when it doesn't actually call encodeFileForEmail
override when using attachFile (I had die()
atop of it at one stage)
Please note that i'm referring to the methods MandrillEmail::sendTemplate() and MandrillMailer::sendTemplate() which doesn't accomodate attachments, it will build the params and call the Mandrill PHP Library's sendTemplate
method without fetching any possible attachments
Adding an extra param $attachFiles = []
to both method signatures of sendTemplate and:
if ($attachFiles) {
$attachments = array();
foreach($attachFiles as $file) {
$attachments[] = $this->encodeFileForEmail($file);
}
$params['attachments'] = $attachments;
}
into the sendTemplate
method of MandrillMailer
was my only solution :(
The initial code above that raised this issue can be fixed now by using the extra param:
$mailer = MandrillEmail::create();
$mailer->setTo($to);
$mailer->setRecipientMetadatas($to, []);
$mailer->setFrom('noreply@example.com.au');
$mailer->setSubject($subject);
$mailer->sendTemplate(
'application-approval',
$mergeVars,
[
'/home/user/public_html/myfile.png'
]
);
Unless you mean just instantiate $mailer = Email::create()
that I did not try
Ah yes indeed, I missed the template part. I've never used this feature so far so it's possible. If it's working for you, I'd be happy to merge this in.
Working for me, will PR
@lekoala any chance you can take a look at that PR and tag out a new ver so I can lock this site to it
sorry i missed the notification, it's merged now
please also note that I'm in the process of splitting this module to center around mandrill and exclude any email template fucntionnality so the master branch may be a bit strange while I do the transition