As I have to send some emails with PDF attachments I thought about writing a mail service that uses PHPMailer (https://github.com/PHPMailer/PHPMailer) to make my life easier… The opposite happened…
This is what I’ve got so far:
// site/plugins/kirby-phpmailer/service.php
<?php
email::$services['phpmailer'] = function($email) {
require_once(__DIR__ . DS . 'class.phpmailer.php');
$mail = new PHPMailer;
$mail->setFrom($email->from);
$mail->addReplyTo($email->replyTo);
$mail->addAddress($email->to);
if ($email->attachment != null) {
$mail->addAttachment($email->attachment);
}
$mail->isHTML(true);
$mail->Subject = $email->subject;
$mail->Body = $email->body;
if (!$mail->send()) {
throw new Error('PHPMailer error: ' . $mail->ErrorInfo);
}
}
// site/plugins/kirby-phpmailer/class.phpmailer.php
<?php
class PHPMailer {
...
}
I guess my problems are not very kirby specific but general PHP knowledge: Can I even require in a file in an anonymus function? If so, what is __DIR__
set to? Would it be better to pass $mail
to the function, i.e. create a closure? And most importantly: Why does whatever goes wrong here not lead to an entry in my server’s error_log
??