5

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 protected]";

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?

2 Answers 2

4

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.

3
  • 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." Commented Jul 30, 2014 at 22:12
  • 2
    addAttachment is a method. use $email->addAttachment($asset_url, 'plugin_name', 'base64', 'application/zip');
    – Brad Bell
    Commented Jul 30, 2014 at 22:14
  • Once I change to $email->addAttachment() the entire function dies. If I remove it, it works. I'll send you a support note. Commented Jul 30, 2014 at 22:53
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?

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.