Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitc9c55dc

Browse files
committed
[Mime] added classes for generating MIME messages
1 parent91c5b14 commitc9c55dc

File tree

98 files changed

+8096
-18
lines changed

Some content is hidden

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

98 files changed

+8096
-18
lines changed
Lines 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+
namespaceSymfony\Bridge\Twig\Mime;
13+
14+
useLeague\HTMLToMarkdown\HtmlConverter;
15+
useTwig\Environment;
16+
17+
class Renderer
18+
{
19+
private$twig;
20+
private$context;
21+
private$converter;
22+
23+
publicfunction__construct(Environment$twig,array$context = [])
24+
{
25+
$this->twig =$twig;
26+
$this->context =$context;
27+
if (class_exists(HtmlConverter::class)) {
28+
$this->converter =newHtmlConverter([
29+
'hard_break' =>true,
30+
'strip_tags' =>true,
31+
'remove_nodes' =>'head style',
32+
]);
33+
}
34+
}
35+
36+
publicfunctionrender(TemplatedEmail$email):TemplatedEmail
37+
{
38+
$email =clone$email;
39+
40+
$vars =array_merge($this->context,$email->getContext(), [
41+
'email' =>newWrappedTemplatedEmail($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+
privatefunctionrenderFull(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+
privatefunctionconvertHtmlToText(string$html):string
88+
{
89+
if (null !==$this->converter) {
90+
return$this->converter->convert($html);
91+
}
92+
93+
returnstrip_tags($html);
94+
}
95+
}
Lines 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+
namespaceSymfony\Bridge\Twig\Mime;
13+
14+
useSymfony\Component\Mime\Email;
15+
16+
class TemplatedEmailextends Email
17+
{
18+
private$template;
19+
private$htmlTemplate;
20+
private$textTemplate;
21+
private$context = [];
22+
23+
publicfunctiontemplate(?string$template):self
24+
{
25+
$this->template =$template;
26+
27+
return$this;
28+
}
29+
30+
publicfunctiontextTemplate(?string$template):self
31+
{
32+
$this->textTemplate =$template;
33+
34+
return$this;
35+
}
36+
37+
publicfunctionhtmlTemplate(?string$template):self
38+
{
39+
$this->htmlTemplate =$template;
40+
41+
return$this;
42+
}
43+
44+
publicfunctiongetTemplate(): ?string
45+
{
46+
return$this->template;
47+
}
48+
49+
publicfunctiongetTextTemplate(): ?string
50+
{
51+
return$this->textTemplate;
52+
}
53+
54+
publicfunctiongetHtmlTemplate(): ?string
55+
{
56+
return$this->htmlTemplate;
57+
}
58+
59+
publicfunctioncontext(array$context):self
60+
{
61+
$this->context =$context;
62+
63+
return$this;
64+
}
65+
66+
publicfunctiongetContext():array
67+
{
68+
return$this->context;
69+
}
70+
}
Lines 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+
namespaceSymfony\Bridge\Twig\Mime;
13+
14+
useSymfony\Component\Mime\Address;
15+
useSymfony\Component\Mime\NamedAddress;
16+
useTwig\Environment;
17+
18+
/**
19+
* @internal
20+
*/
21+
finalclass WrappedTemplatedEmail
22+
{
23+
private$twig;
24+
private$message;
25+
26+
publicfunction__construct(Environment$twig,TemplatedEmail$message)
27+
{
28+
$this->twig =$twig;
29+
$this->message =$message;
30+
}
31+
32+
publicfunctiontoName():string
33+
{
34+
$to =$this->message->getTo()[0];
35+
36+
return$toinstanceof NamedAddress ?$to->getName() :'';
37+
}
38+
39+
publicfunctionimage(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+
publicfunctionattach(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+
publicfunctionsetSubject(string$subject):self
62+
{
63+
$this->message->subject($subject);
64+
65+
return$this;
66+
}
67+
68+
publicfunctiongetSubject(): ?string
69+
{
70+
return$this->message->getSubject();
71+
}
72+
73+
publicfunctionsetReturnPath(string$address):self
74+
{
75+
$this->message->returnPath($address);
76+
77+
return$this;
78+
}
79+
80+
publicfunctiongetReturnPath():string
81+
{
82+
return$this->message->getReturnPath();
83+
}
84+
85+
publicfunctionaddFrom(string$address,string$name =null):self
86+
{
87+
$this->message->addFrom($name ?newNamedAddress($address,$name) :newAddress($address));
88+
89+
return$this;
90+
}
91+
92+
/**
93+
* @return (Address|NamedAddress)[]
94+
*/
95+
publicfunctiongetFrom():array
96+
{
97+
return$this->message->getFrom();
98+
}
99+
100+
publicfunctionaddReplyTo(string$address):self
101+
{
102+
$this->message->addReplyTo($address);
103+
104+
return$this;
105+
}
106+
107+
/**
108+
* @return Address[]
109+
*/
110+
publicfunctiongetReplyTo():array
111+
{
112+
return$this->message->getReplyTo();
113+
}
114+
115+
publicfunctionaddTo(string$address,string$name =null):self
116+
{
117+
$this->message->addTo($name ?newNamedAddress($address,$name) :newAddress($address));
118+
119+
return$this;
120+
}
121+
122+
/**
123+
* @return (Address|NamedAddress)[]
124+
*/
125+
publicfunctiongetTo():array
126+
{
127+
return$this->message->getTo();
128+
}
129+
130+
publicfunctionaddCc(string$address,string$name =null):self
131+
{
132+
$this->message->addCc($name ?newNamedAddress($address,$name) :newAddress($address));
133+
134+
return$this;
135+
}
136+
137+
/**
138+
* @return (Address|NamedAddress)[]
139+
*/
140+
publicfunctiongetCc():array
141+
{
142+
return$this->message->getCc();
143+
}
144+
145+
publicfunctionaddBcc(string$address,string$name =null):self
146+
{
147+
$this->message->addBcc($name ?newNamedAddress($address,$name) :newAddress($address));
148+
149+
return$this;
150+
}
151+
152+
/**
153+
* @return (Address|NamedAddress)[]
154+
*/
155+
publicfunctiongetBcc():array
156+
{
157+
return$this->message->getBcc();
158+
}
159+
160+
publicfunctionsetPriority(int$priority):self
161+
{
162+
$this->message->setPriority($priority);
163+
164+
return$this;
165+
}
166+
167+
publicfunctiongetPriority():int
168+
{
169+
return$this->message->getPriority();
170+
}
171+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp