/** * @dataProvider replaceDataProvider * @param $bodyTemplate * @param array $attachments */ public function testReplace($bodyTemplate, array $attachments) { $email = new Email(); $emailBody = new EmailBody(); $replacements = []; $contentIds = []; foreach ($attachments as $attachmentData) { $attachment = new EmailAttachment(); $emailAttachmentContent = new EmailAttachmentContent(); $emailAttachmentContent->setContent($attachmentData['content'])->setContentTransferEncoding($attachmentData['transfer_encoding']); $attachment->setEmbeddedContentId($attachmentData['content_id'])->setContentType($attachmentData['type'])->setContent($emailAttachmentContent); $emailBody->addAttachment($attachment); $cid = 'cid:' . $attachmentData['content_id']; $contentIds[] = $cid; if ($attachmentData['replace']) { $replacements[] = sprintf('data:%s;base64,%s', $attachmentData['type'], $attachmentData['content']); } else { $replacements[] = $cid; } } $emailBody->setBodyContent(vsprintf($bodyTemplate, $contentIds)); $email->setEmailBody($emailBody); $event = new EmailBodyLoaded($email); $this->listener->replace($event); $this->assertEquals($email, $event->getEmail()); $this->assertEquals(vsprintf($bodyTemplate, $replacements), $event->getEmail()->getEmailBody()->getBodyContent()); }
/** * Adds an email attachment * * @param string $fileName * @param string $content * @param string $contentType * @param string $contentTransferEncoding * @throws \LogicException */ public function addEmailAttachment($fileName, $content, $contentType, $contentTransferEncoding) { if ($this->emailBody === null) { throw new \LogicException('Call setEmailBody first.'); } $emailAttachment = new EmailAttachment(); $emailAttachmentContent = new EmailAttachmentContent(); $emailAttachmentContent->setEmailAttachment($emailAttachment)->setContentTransferEncoding($contentTransferEncoding)->setValue($content); $emailAttachment->setFileName($fileName)->setContentType($contentType)->setContent($emailAttachmentContent); $this->emailBody->addAttachment($emailAttachment); }
/** * Adds an email attachment * * @param string $fileName * @param string $content * @param string $contentType * @param string $contentTransferEncoding * @param null|string $embeddedContentId * @param null|int $contentSize * * @throws \LogicException */ public function addEmailAttachment($fileName, $content, $contentType, $contentTransferEncoding, $embeddedContentId = null, $contentSize = null) { if ($this->emailBody === null) { throw new \LogicException('Call setEmailBody first.'); } if (!$this->allowSaveAttachment($this->checkContentSizeValue($content, $contentSize, $contentTransferEncoding))) { return; } $emailAttachment = new EmailAttachment(); $emailAttachmentContent = new EmailAttachmentContent(); $emailAttachmentContent->setEmailAttachment($emailAttachment)->setContentTransferEncoding($contentTransferEncoding)->setContent($content); $emailAttachment->setFileName($fileName)->setContentType($contentType)->setContent($emailAttachmentContent)->setEmbeddedContentId($embeddedContentId); $this->emailBody->addAttachment($emailAttachment); }
public function testContentTransferEncodingGetterAndSetter() { $entity = new EmailAttachmentContent(); $entity->setContentTransferEncoding('test'); $this->assertEquals('test', $entity->getContentTransferEncoding()); }
/** * Set content of email attachment * * @param EmailAttachmentContent $attachmentContent * @return $this */ public function setContent(EmailAttachmentContent $attachmentContent) { $this->attachmentContent = $attachmentContent; $attachmentContent->setEmailAttachment($this); return $this; }
/** * @param UploadedFile $uploadedFile * * @return AttachmentEntity */ public function entityFromUploadedFile(UploadedFile $uploadedFile) { $emailAttachment = new AttachmentEntity(); $attachmentContent = new EmailAttachmentContent(); $attachmentContent->setContent(base64_encode(file_get_contents($uploadedFile->getRealPath()))); $attachmentContent->setContentTransferEncoding('base64'); $attachmentContent->setEmailAttachment($emailAttachment); $emailAttachment->setContent($attachmentContent); $emailAttachment->setContentType($uploadedFile->getMimeType()); $emailAttachment->setFileName($uploadedFile->getClientOriginalName()); return $emailAttachment; }
/** * Process inline images. Convert it to embedded attachments and update message body. * * @param \Swift_Message $message * @param EmailModel $model */ protected function processEmbeddedImages(\Swift_Message $message, EmailModel $model) { if ($model->getType() === 'html') { $guesser = ExtensionGuesser::getInstance(); $body = $message->getBody(); $body = preg_replace_callback('/<img(.*)src(\\s*)=(\\s*)["\'](.*)["\']/U', function ($matches) use($message, $guesser, $model) { if (count($matches) === 5) { // 1st match contains any data between '<img' and 'src' parts (e.g. 'width=100') $imgConfig = $matches[1]; // 4th match contains src attribute value $srcData = $matches[4]; if (strpos($srcData, 'data:image') === 0) { list($mime, $content) = explode(';', $srcData); list($encoding, $file) = explode(',', $content); $mime = str_replace('data:', '', $mime); $fileName = sprintf('%s.%s', uniqid(), $guesser->guess($mime)); $swiftAttachment = \Swift_Image::newInstance(ContentDecoder::decode($file, $encoding), $fileName, $mime); /** @var $message \Swift_Message */ $id = $message->embed($swiftAttachment); $attachmentContent = new EmailAttachmentContent(); $attachmentContent->setContent($file); $attachmentContent->setContentTransferEncoding($encoding); $emailAttachment = new EmailAttachment(); $emailAttachment->setEmbeddedContentId($swiftAttachment->getId()); $emailAttachment->setFileName($fileName); $emailAttachment->setContentType($mime); $attachmentContent->setEmailAttachment($emailAttachment); $emailAttachment->setContent($attachmentContent); $emailAttachmentModel = new EmailAttachmentModel(); $emailAttachmentModel->setEmailAttachment($emailAttachment); $model->addAttachment($emailAttachmentModel); return sprintf('<img%ssrc="%s"', $imgConfig, $id); } } }, $body); $message->setBody($body, 'text/html'); } }
/** * Create EmailAttachmentContent entity object * * @param string $content The attachment content encoded as it is specified in $contentTransferEncoding parameter * @param string $contentTransferEncoding The attachment content encoding type * @return EmailAttachmentContent */ public function attachmentContent($content, $contentTransferEncoding) { $result = new EmailAttachmentContent(); $result->setValue($content)->setContentTransferEncoding($contentTransferEncoding); return $result; }