Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
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 Jul 30 at 22:12
2  
addAttachment is a method. use $email->addAttachment($asset_url, 'plugin_name', 'base64', 'application/zip'); –  Brad Bell Jul 30 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. –  Adam McCombs Jul 30 at 22:53

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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