<p>Craft 2 uses the <a href="https://github.com/PHPMailer/PHPMailer" rel="nofollow noreferrer">PHPMailer</a> library to send emails and the first thing its <a href="https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php#L2776-L2778" rel="nofollow noreferrer">addAttachment()</a> method does is take the first <code>$path</code> parameter that's passed in and run PHP's <code>is_file()</code> on it.</p>

<p><code>is_file()</code> 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.</p>

<p>It won't show up on the model as an error using <code>$email-&gt;getErrors()</code> 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.</p>

CC BY-SA 4.0
2018-05-10T22:41:04.143Z
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?
Clive Portman
CC BY-SA 4.0
2018-05-10T22:47:22.993Z
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
CC BY-SA 4.0
2018-05-10T22:54:21.350Z
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.
Clive Portman
CC BY-SA 4.0
2018-05-10T21:59:54.417Z
2018-05-10T21:59:54.417Z
Brad Bell
<p>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?</p>

<pre><code>$email = new EmailModel();
$email-&gt;toEmail = $toEmail;
$email-&gt;cc = $ccEmail;
$email-&gt;bcc = $bccEmail;
$email-&gt;subject = $subject;
$email-&gt;body = $body;
//$email-&gt;attachments = $attachments;

$attachments = [
  ['path'=&gt;'http://website.localhost/img/uploads/nameoffile.pdf',
  'name'=&gt;'afile',
  'encoding'=&gt;'base64',
  'type'=&gt;'application/pdf']
];
if (!empty($attachments)) {
  foreach ($attachments as $attachment) {
    if ($attachment) {
      $email-&gt;addAttachment($attachment['path'], $attachment['name'], 'base64', $attachment['type']);
    }
  }
}

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

// SEND THE MAIL
if (craft()-&gt;email-&gt;sendEmail($email)) return true;
else throw new Exception('not sent'); // returns this exception
</code></pre>

CC BY-SA 4.0
2018-05-10T16:13:49.080Z
Possible duplicate of [Best way to include attachments using EmailService \[Craft Plugin\]?](https://craftcms.stackexchange.com/questions/1533/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()`
Robin Schambach
CC BY-SA 4.0
2018-05-10T16:36:29.310Z
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.
Clive Portman
<plugin-development><emails>
Sending attachments within a plugin
# Sending attachments within a plugin

(Asked by: username_0 on 2018-05-10)

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 = [
  ['path'=>'http://website.localhost/img/uploads/nameoffile.pdf',
  'name'=>'afile',
  'encoding'=>'base64',
  'type'=>'application/pdf']
];
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
} 

// SEND THE MAIL
if (craft()->email->sendEmail($email)) return true;
else throw new Exception('not sent'); // returns this exception

**Tags:** plugin-development, emails
> **Comments:**
> **username_1**: Possible duplicate of [Best way to include attachments using EmailService \[Craft Plugin\]?](https://craftcms.stackexchange.com/questions/1533/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()`  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_2 on 2018-05-10. Score: 1

Craft 2 uses the [PHPMailer](https://github.com/PHPMailer/PHPMailer) library to send emails and the first thing its [addAttachment()](https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php#L2776-L2778) 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.

> **Comments:**
> **username_0**: 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?  
> **username_2**: 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`?  
> **username_0**: 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.  

---

