Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T22:12:07.433Z
Answers.Text: 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."
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T22:14:37.470Z
Answers.Text: addAttachment is a method.  use $email->addAttachment($asset_url, 'plugin_name', 'base64', 'application/zip');
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T22:53:43.267Z
Answers.Text: Once I change to $email->addAttachment() the entire function dies. If I remove it, it works. I'll send you a support note.
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T21:44:05.567Z
Answers.LastActivityDate: 2014-07-30T21:44:05.567Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T21:40:12.510Z
Answers.LastActivityDate: 2014-07-30T21:40:12.510Z
Answers.UserDisplayName: Sam Hernandez
Body: <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>
  
Tags: <plugin-development>
Title: Best way to include attachments using EmailService [Craft Plugin]?
ThreadText: # 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 = "<h1>Congratulations!</h1";
      $email->htmlBody .= "<p>Hello World</p>";
      $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](https://github.com/pixelandtonic/ContactForm/blob/master/contactform/services/ContactFormService.php#L51) 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](https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L2047) 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?
  
  
  ---
  
  