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

Commit c9c55dc

Browse filesBrowse files
committed
[Mime] added classes for generating MIME messages
1 parent 91c5b14 commit c9c55dc
Copy full SHA for c9c55dc

File tree

98 files changed

+8096
-18
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

98 files changed

+8096
-18
lines changed
+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Mime;
13+
14+
use League\HTMLToMarkdown\HtmlConverter;
15+
use Twig\Environment;
16+
17+
class Renderer
18+
{
19+
private $twig;
20+
private $context;
21+
private $converter;
22+
23+
public function __construct(Environment $twig, array $context = [])
24+
{
25+
$this->twig = $twig;
26+
$this->context = $context;
27+
if (class_exists(HtmlConverter::class)) {
28+
$this->converter = new HtmlConverter([
29+
'hard_break' => true,
30+
'strip_tags' => true,
31+
'remove_nodes' => 'head style',
32+
]);
33+
}
34+
}
35+
36+
public function render(TemplatedEmail $email): TemplatedEmail
37+
{
38+
$email = clone $email;
39+
40+
$vars = array_merge($this->context, $email->getContext(), [
41+
'email' => new WrappedTemplatedEmail($this->twig, $email),
42+
]);
43+
44+
if ($template = $email->getTemplate()) {
45+
$this->renderFull($email, $template, $vars);
46+
}
47+
48+
if ($template = $email->getTextTemplate()) {
49+
$email->text($this->twig->render($template, $vars));
50+
}
51+
52+
if ($template = $email->getHtmlTemplate()) {
53+
$email->html($this->twig->render($template, $vars));
54+
}
55+
56+
// if text body is empty, compute one from the HTML body
57+
if (!$email->getTextBody() && null !== $html = $email->getHtmlBody()) {
58+
$email->text($this->convertHtmlToText(\is_resource($html) ? stream_get_contents($html) : $html));
59+
}
60+
61+
return $email;
62+
}
63+
64+
private function renderFull(TemplatedEmail $email, string $template, array $vars): void
65+
{
66+
$template = $this->twig->load($template);
67+
68+
if ($template->hasBlock('subject', $vars)) {
69+
$email->subject($template->renderBlock('subject', $vars));
70+
}
71+
72+
if ($template->hasBlock('text', $vars)) {
73+
$email->text($template->renderBlock('text', $vars));
74+
}
75+
76+
if ($template->hasBlock('html', $vars)) {
77+
$email->html($template->renderBlock('html', $vars));
78+
}
79+
80+
if ($template->hasBlock('config', $vars)) {
81+
// we discard the output as we're only interested
82+
// in the side effect of calling email methods
83+
$template->renderBlock('config', $vars);
84+
}
85+
}
86+
87+
private function convertHtmlToText(string $html): string
88+
{
89+
if (null !== $this->converter) {
90+
return $this->converter->convert($html);
91+
}
92+
93+
return strip_tags($html);
94+
}
95+
}
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Mime;
13+
14+
use Symfony\Component\Mime\Email;
15+
16+
class TemplatedEmail extends Email
17+
{
18+
private $template;
19+
private $htmlTemplate;
20+
private $textTemplate;
21+
private $context = [];
22+
23+
public function template(?string $template): self
24+
{
25+
$this->template = $template;
26+
27+
return $this;
28+
}
29+
30+
public function textTemplate(?string $template): self
31+
{
32+
$this->textTemplate = $template;
33+
34+
return $this;
35+
}
36+
37+
public function htmlTemplate(?string $template): self
38+
{
39+
$this->htmlTemplate = $template;
40+
41+
return $this;
42+
}
43+
44+
public function getTemplate(): ?string
45+
{
46+
return $this->template;
47+
}
48+
49+
public function getTextTemplate(): ?string
50+
{
51+
return $this->textTemplate;
52+
}
53+
54+
public function getHtmlTemplate(): ?string
55+
{
56+
return $this->htmlTemplate;
57+
}
58+
59+
public function context(array $context): self
60+
{
61+
$this->context = $context;
62+
63+
return $this;
64+
}
65+
66+
public function getContext(): array
67+
{
68+
return $this->context;
69+
}
70+
}
+171Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Mime;
13+
14+
use Symfony\Component\Mime\Address;
15+
use Symfony\Component\Mime\NamedAddress;
16+
use Twig\Environment;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class WrappedTemplatedEmail
22+
{
23+
private $twig;
24+
private $message;
25+
26+
public function __construct(Environment $twig, TemplatedEmail $message)
27+
{
28+
$this->twig = $twig;
29+
$this->message = $message;
30+
}
31+
32+
public function toName(): string
33+
{
34+
$to = $this->message->getTo()[0];
35+
36+
return $to instanceof NamedAddress ? $to->getName() : '';
37+
}
38+
39+
public function image(string $image, string $contentType = null): string
40+
{
41+
$file = $this->twig->getLoader()->getSourceContext($image);
42+
if ($path = $file->getPath()) {
43+
$this->message->embedFromPath($path, $image, $contentType);
44+
} else {
45+
$this->message->embed($file->getCode(), $image, $contentType);
46+
}
47+
48+
return 'cid:'.$image;
49+
}
50+
51+
public function attach(string $file, string $name = null, string $contentType = null): void
52+
{
53+
$file = $this->twig->getLoader()->getSourceContext($file);
54+
if ($path = $file->getPath()) {
55+
$this->message->attachFromPath($path, $name, $contentType);
56+
} else {
57+
$this->message->attach($file->getCode(), $name, $contentType);
58+
}
59+
}
60+
61+
public function setSubject(string $subject): self
62+
{
63+
$this->message->subject($subject);
64+
65+
return $this;
66+
}
67+
68+
public function getSubject(): ?string
69+
{
70+
return $this->message->getSubject();
71+
}
72+
73+
public function setReturnPath(string $address): self
74+
{
75+
$this->message->returnPath($address);
76+
77+
return $this;
78+
}
79+
80+
public function getReturnPath(): string
81+
{
82+
return $this->message->getReturnPath();
83+
}
84+
85+
public function addFrom(string $address, string $name = null): self
86+
{
87+
$this->message->addFrom($name ? new NamedAddress($address, $name) : new Address($address));
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* @return (Address|NamedAddress)[]
94+
*/
95+
public function getFrom(): array
96+
{
97+
return $this->message->getFrom();
98+
}
99+
100+
public function addReplyTo(string $address): self
101+
{
102+
$this->message->addReplyTo($address);
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* @return Address[]
109+
*/
110+
public function getReplyTo(): array
111+
{
112+
return $this->message->getReplyTo();
113+
}
114+
115+
public function addTo(string $address, string $name = null): self
116+
{
117+
$this->message->addTo($name ? new NamedAddress($address, $name) : new Address($address));
118+
119+
return $this;
120+
}
121+
122+
/**
123+
* @return (Address|NamedAddress)[]
124+
*/
125+
public function getTo(): array
126+
{
127+
return $this->message->getTo();
128+
}
129+
130+
public function addCc(string $address, string $name = null): self
131+
{
132+
$this->message->addCc($name ? new NamedAddress($address, $name) : new Address($address));
133+
134+
return $this;
135+
}
136+
137+
/**
138+
* @return (Address|NamedAddress)[]
139+
*/
140+
public function getCc(): array
141+
{
142+
return $this->message->getCc();
143+
}
144+
145+
public function addBcc(string $address, string $name = null): self
146+
{
147+
$this->message->addBcc($name ? new NamedAddress($address, $name) : new Address($address));
148+
149+
return $this;
150+
}
151+
152+
/**
153+
* @return (Address|NamedAddress)[]
154+
*/
155+
public function getBcc(): array
156+
{
157+
return $this->message->getBcc();
158+
}
159+
160+
public function setPriority(int $priority): self
161+
{
162+
$this->message->setPriority($priority);
163+
164+
return $this;
165+
}
166+
167+
public function getPriority(): int
168+
{
169+
return $this->message->getPriority();
170+
}
171+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.