Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Mime messages #30416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"symfony/contracts": "^1.0.2",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/polyfill-intl-idn": "^1.10",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php72": "~1.5",
"symfony/polyfill-php73": "^1.8"
Expand Down
100 changes: 100 additions & 0 deletions 100 src/Symfony/Bridge/Twig/Mime/Renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Mime;

use League\HTMLToMarkdown\HtmlConverter;
use Twig\Environment;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 4.3
*/
final class Renderer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the class used? Or where do we plan to use it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you were at my keynote, listening carefully :) Everything is explained in the presentation linked in the description of the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my core point is related to the design of the class :)
if it's final and has no interface, then type-hinting for it means creating strong coupling, while it could be usefull to provide an alternative implementation (eg another html-to-txt - I used to use w3m for that job and it does it nicely)

{
private $twig;
private $context;
private $converter;

public function __construct(Environment $twig, array $context = [])
{
$this->twig = $twig;
$this->context = $context;
if (class_exists(HtmlConverter::class)) {
fabpot marked this conversation as resolved.
Show resolved Hide resolved
$this->converter = new HtmlConverter([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could we provide autodiscovery for this?
A "suggest" entry in composer.json maybe, but that's barely visible.
I don't have a better idea. (making it a requirement with runtime suggestion for the package?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a good place to start - throw an exception when/if it's used (in convertHtmlToText()). So, if you set the text & html body, you'll never need it and never get an exception about missing it. But if you do, we say:

Cannot automatically convert HTML body to a text body. Run "composer require league/html-to-markdown"
or set the text content explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your suggestion @weaverryan, can you create a PR for it?

'hard_break' => true,
'strip_tags' => true,
'remove_nodes' => 'head style',
]);
}
}

public function render(TemplatedEmail $email): TemplatedEmail
{
$email = clone $email;

$vars = array_merge($this->context, $email->getContext(), [
'email' => new WrappedTemplatedEmail($this->twig, $email),
]);

if ($template = $email->getTemplate()) {
$this->renderFull($email, $template, $vars);
}

if ($template = $email->getTextTemplate()) {
$email->text($this->twig->render($template, $vars));
}

if ($template = $email->getHtmlTemplate()) {
$email->html($this->twig->render($template, $vars));
}

// if text body is empty, compute one from the HTML body
if (!$email->getTextBody() && null !== $html = $email->getHtmlBody()) {
$email->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
}

return $email;
}

private function renderFull(TemplatedEmail $email, string $template, array $vars): void
{
$template = $this->twig->load($template);

if ($template->hasBlock('subject', $vars)) {
$email->subject($template->renderBlock('subject', $vars));
}

if ($template->hasBlock('text', $vars)) {
$email->text($template->renderBlock('text', $vars));
}

if ($template->hasBlock('html', $vars)) {
$email->html($template->renderBlock('html', $vars));
}

if ($template->hasBlock('config', $vars)) {
// we discard the output as we're only interested
// in the side effect of calling email methods
$template->renderBlock('config', $vars);
}
}

private function convertHtmlToText(string $html): string
{
if (null !== $this->converter) {
return $this->converter->convert($html);
}

return strip_tags($html);
}
}
87 changes: 87 additions & 0 deletions 87 src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Mime;

use Symfony\Component\Mime\Email;

/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 4.3
*/
class TemplatedEmail extends Email
{
private $template;
private $htmlTemplate;
private $textTemplate;
private $context = [];

/**
* @return $this
*/
public function template(?string $template)
{
$this->template = $template;

return $this;
}

/**
* @return $this
*/
public function textTemplate(?string $template)
{
$this->textTemplate = $template;

return $this;
}

/**
* @return $this
*/
public function htmlTemplate(?string $template)
{
$this->htmlTemplate = $template;

return $this;
}

public function getTemplate(): ?string
{
return $this->template;
}

public function getTextTemplate(): ?string
{
return $this->textTemplate;
}

public function getHtmlTemplate(): ?string
{
return $this->htmlTemplate;
}

/**
* @return $this
*/
public function context(array $context)
{
$this->context = $context;

return $this;
}

public function getContext(): array
{
return $this->context;
}
}
199 changes: 199 additions & 0 deletions 199 src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Mime;

use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\NamedAddress;
use Twig\Environment;

/**
* @internal
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 4.3
*/
final class WrappedTemplatedEmail
{
private $twig;
private $message;

public function __construct(Environment $twig, TemplatedEmail $message)
{
$this->twig = $twig;
$this->message = $message;
}

public function toName(): string
{
$to = $this->message->getTo()[0];

return $to instanceof NamedAddress ? $to->getName() : '';
}

public function image(string $image, string $contentType = null): string
{
$file = $this->twig->getLoader()->getSourceContext($image);
if ($path = $file->getPath()) {
$this->message->embedFromPath($path, $image, $contentType);
} else {
$this->message->embed($file->getCode(), $image, $contentType);
}

return 'cid:'.$image;
}

public function attach(string $file, string $name = null, string $contentType = null): void
{
$file = $this->twig->getLoader()->getSourceContext($file);
if ($path = $file->getPath()) {
$this->message->attachFromPath($path, $name, $contentType);
} else {
$this->message->attach($file->getCode(), $name, $contentType);
}
}

/**
* @return $this
*/
public function setSubject(string $subject)
{
$this->message->subject($subject);

return $this;
}

public function getSubject(): ?string
{
return $this->message->getSubject();
}

/**
* @return $this
*/
public function setReturnPath(string $address)
{
$this->message->returnPath($address);

return $this;
}

public function getReturnPath(): string
{
return $this->message->getReturnPath();
}

/**
* @return $this
*/
public function addFrom(string $address, string $name = null)
{
$this->message->addFrom($name ? new NamedAddress($address, $name) : new Address($address));

return $this;
}

/**
* @return (Address|NamedAddress)[]
*/
public function getFrom(): array
{
return $this->message->getFrom();
}

/**
* @return $this
*/
public function addReplyTo(string $address)
{
$this->message->addReplyTo($address);

return $this;
}

/**
* @return Address[]
*/
public function getReplyTo(): array
{
return $this->message->getReplyTo();
}

/**
* @return $this
*/
public function addTo(string $address, string $name = null)
{
$this->message->addTo($name ? new NamedAddress($address, $name) : new Address($address));

return $this;
}

/**
* @return (Address|NamedAddress)[]
*/
public function getTo(): array
{
return $this->message->getTo();
}

/**
* @return $this
*/
public function addCc(string $address, string $name = null)
{
$this->message->addCc($name ? new NamedAddress($address, $name) : new Address($address));

return $this;
}

/**
* @return (Address|NamedAddress)[]
*/
public function getCc(): array
{
return $this->message->getCc();
}

/**
* @return $this
*/
public function addBcc(string $address, string $name = null)
{
$this->message->addBcc($name ? new NamedAddress($address, $name) : new Address($address));

return $this;
}

/**
* @return (Address|NamedAddress)[]
*/
public function getBcc(): array
{
return $this->message->getBcc();
}

/**
* @return $this
*/
public function setPriority(int $priority)
{
$this->message->setPriority($priority);

return $this;
}

public function getPriority(): int
{
return $this->message->getPriority();
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.