<p>The <a href="https://github.com/pixelandtonic/ContactForm/blob/master/contactform/services/ContactFormService.php#L51" rel="nofollow">Contact Form</a> plugin has an example of doing this.</p>

<pre><code>$email-&gt;addAttachment($message-&gt;attachment-&gt;getTempName(), $message-&gt;attachment-&gt;getName(), 'base64', $message-&gt;attachment-&gt;getType());
</code></pre>

<p>The <code>EmailModel-&gt;addAttachment()</code> signature looks like this:</p>

<pre><code>* @param string $path Path to the attachment.
* @param string $name Overrides the attachment name.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.
</code></pre>

<p>Which is just a thin wrapper for <a href="https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L2047" rel="nofollow">PHPMailer's addAttachment</a> method.</p>

CC BY-SA 3.0
2014-07-30T22:12:07.433Z
I'm using $asset_url = craft()->assets->getUrlForFile($asset); and then $email->addAttachment = array($asset_url, 'plugin_name', 'base64', 'application/zip'); but it returns "Property "Craft\EmailModel.addAttachment" is not defined."
Adam McCombs
CC BY-SA 3.0
2014-07-30T22:14:37.470Z
addAttachment is a method.  use $email->addAttachment($asset_url, 'plugin_name', 'base64', 'application/zip');
Brad Bell
CC BY-SA 3.0
2014-07-30T22:53:43.267Z
Once I change to $email->addAttachment() the entire function dies. If I remove it, it works. I'll send you a support note.
Adam McCombs
CC BY-SA 3.0
2014-07-30T21:44:05.567Z
2014-07-30T21:44:05.567Z
Brad Bell
<p>In <code>craft/app/models/EmailModel.php</code> the function docblock describes the params:</p>

<pre><code>/**
 * Adds an attachment from a path on the filesystem. Returns false if the file could not be found or accessed.
 *
 * @param string $path Path to the attachment.
 * @param string $name Overrides the attachment name.
 * @param string $encoding File encoding (see $Encoding).
 * @param string $type File extension (MIME) type.
 * @throws phpmailerException
 * @return bool
 */
public function addAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream')
{
    $existingAttachments = $this-&gt;attachments;
    $existingAttachments[] = array('path' =&gt; $path, 'name' =&gt; $name, 'encoding' =&gt; $encoding, 'type' =&gt; $type);
    $this-&gt;attachments = $existingAttachments;
}
</code></pre>

<p>Is that what you're looking for?</p>

CC BY-SA 3.0
2014-07-30T21:40:12.510Z
2014-07-30T21:40:12.510Z
Sam Hernandez
<p>I've got Email Service working correctly (<a href="http://buildwithcraft.com/docs/apis/emailservice" rel="noreferrer">http://buildwithcraft.com/docs/apis/emailservice</a>) but what's the best way to include attachments when using this service? </p>

<p>Here's what I've got so far:</p>

<pre><code>    $email = new EmailModel();
    $email-&gt;subject = "Thanks!";
    $email-&gt;htmlBody = "&lt;h1&gt;Congratulations!&lt;/h1";
    $email-&gt;htmlBody .= "&lt;p&gt;Hello World&lt;/p&gt;";
    $email-&gt;fromEmail = "email@domain.com";
</code></pre>

<p>I see there is <strong>$email->addAttachment</strong> but I'm not sure what variables to include to make it pass through the service.</p>

<p>Anyone have any examples?</p>

<plugin-development>
Best way to include attachments using EmailService [Craft Plugin]?
# Best way to include attachments using EmailService [Craft Plugin]?

(Asked by: username_0 on 2014-07-30)

I've got Email Service working correctly (http://buildwithcraft.com/docs/apis/emailservice) but what's the best way to include attachments when using this service? 
Here's what I've got so far:
    $email = new EmailModel();
    $email->subject = "Thanks!";
    $email->htmlBody = "Congratulations!htmlBody .= "Hello World";
    $email->fromEmail = "email@domain.com";

I see there is $email->addAttachment but I'm not sure what variables to include to make it pass through the service.
Anyone have any examples?

**Tags:** plugin-development


---

## 2 answers

### Answer by username_1 on 2014-07-30. Score: 4 | Accepted answer

The Contact Form plugin has an example of doing this.
$email->addAttachment($message->attachment->getTempName(), $message->attachment->getName(), 'base64', $message->attachment->getType());

The EmailModel->addAttachment() signature looks like this:
* @param string $path Path to the attachment.
* @param string $name Overrides the attachment name.
* @param string $encoding File encoding (see $Encoding).
* @param string $type File extension (MIME) type.

Which is just a thin wrapper for PHPMailer's addAttachment method.

> **Comments:**
> **username_0**: I'm using $asset_url = craft()->assets->getUrlForFile($asset); and then $email->addAttachment = array($asset_url, 'plugin_name', 'base64', 'application/zip'); but it returns "Property "Craft\EmailModel.addAttachment" is not defined."  
> **username_1**: addAttachment is a method.  use $email->addAttachment($asset_url, 'plugin_name', 'base64', 'application/zip');  
> **username_0**: Once I change to $email->addAttachment() the entire function dies. If I remove it, it works. I'll send you a support note.  

---

### Answer by username_2 on 2014-07-30. Score: 5

In craft/app/models/EmailModel.php the function docblock describes the params:
/**
 * Adds an attachment from a path on the filesystem. Returns false if the file could not be found or accessed.
 *
 * @param string $path Path to the attachment.
 * @param string $name Overrides the attachment name.
 * @param string $encoding File encoding (see $Encoding).
 * @param string $type File extension (MIME) type.
 * @throws phpmailerException
 * @return bool
 */
public function addAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream')
{
    $existingAttachments = $this->attachments;
    $existingAttachments[] = array('path' => $path, 'name' => $name, 'encoding' => $encoding, 'type' => $type);
    $this->attachments = $existingAttachments;
}

Is that what you're looking for?


---

