Using Craft 2. Here's my code, which sends the email fine if I remove the attachment part. Can anyone point me in the right direction?

$email = new EmailModel();
$email->toEmail = $toEmail;
$email->cc = $ccEmail;
$email->bcc = $bccEmail;
$email->subject = $subject;
$email->body = $body;
//$email->attachments = $attachments;

$attachments = [
if (!empty($attachments)) {
  foreach ($attachments as $attachment) {
    if ($attachment) {
      $email->addAttachment($attachment['path'], $attachment['name'], 'base64', $attachment['type']);

if ($email->getErrors()) {
  throw new Exception('there are errors'); // exception not returned

if (craft()->email->sendEmail($email)) return true;
else throw new Exception('not sent'); // returns this exception
  • Possible duplicate of Best way to include attachments using EmailService [Craft Plugin]?? Make sure your file paths are correct. You can get error of your model with $email->getErrors() May 10, 2018 at 16:13
  • I'm looking for specific help that goes beyond a How-to, which is all that question offers, so I don't think it's a duplicate. Would you mind showing how you would test for the email errors as I've tried a number of ways and not been able to get any to show, yet sendEmail never returns true. Maybe you could advise on what the path should look like to. If it helps, I'm only running this on local machine using WAMPserver. May 10, 2018 at 16:36

1 Answer 1


Craft 2 uses the PHPMailer library to send emails and the first thing its addAttachment() method does is take the first $path parameter that's passed in and run PHP's is_file() on it.

is_file() won't work on URLs like you're passing in, it expects the file to be on the local file system, so you'll need to download it locally and pass in that path.

It won't show up on the model as an error using $email->getErrors() because the library catches the exception, does some internal logging and returns false. Craft doesn't have a change to add that particular error back on its EmailModel.

  • Thank you. The URL in question is a URL on the same site. I've tried numerous paths and still the attachment fails. I'll keep plugging away with different paths. You don't have an example path that would work from a typical installation you could add to your answer do you? May 10, 2018 at 22:41
  • Even if it's a URL on the same site, I don't think it will work. Have you tried a file system path? i.e. /path/to/file.pdf?
    – Brad Bell
    May 10, 2018 at 22:47
  • Yes, but I'll repeat on a proper server in the morning and see if that helps. I was using the same file system path as defined for the asset source and that wasn't working either. May 10, 2018 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.