@@ -20,6 +20,8 @@ class SMTP extends EmailAdapter
2020 * @param bool $smtpAutoTLS Enable/disable SMTP AutoTLS feature. Defaults to false.
2121 * @param string $xMailer The value to use for the X-Mailer header.
2222 * @param int $timeout SMTP timeout in seconds.
23+ * @param bool $keepAlive Whether to reuse the SMTP connection across process() calls.
24+ * @param int $timelimit SMTP command timelimit in seconds.
2325 */
2426 public function __construct (
2527 private string $ host ,
@@ -29,13 +31,17 @@ public function __construct(
2931 private string $ smtpSecure = '' ,
3032 private bool $ smtpAutoTLS = false ,
3133 private string $ xMailer = '' ,
32- private int $ timeout = 30
34+ private int $ timeout = 30 ,
35+ private bool $ keepAlive = false ,
36+ private int $ timelimit = 30 ,
3337 ) {
3438 if (!\in_array ($ this ->smtpSecure , ['' , 'ssl ' , 'tls ' ])) {
3539 throw new \InvalidArgumentException ('Invalid SMTP secure prefix. Must be "", "ssl" or "tls" ' );
3640 }
3741 }
3842
43+ private ?PHPMailer $ mail = null ;
44+
3945 public function getName (): string
4046 {
4147 return static ::NAME ;
@@ -52,18 +58,33 @@ public function getMaxMessagesPerRequest(): int
5258 protected function process (EmailMessage $ message ): array
5359 {
5460 $ response = new Response ($ this ->getType ());
55- $ mail = new PHPMailer ();
56- $ mail ->isSMTP ();
61+
62+ if ($ this ->keepAlive && $ this ->mail !== null ) {
63+ $ mail = $ this ->mail ;
64+ $ mail ->clearAllRecipients ();
65+ $ mail ->clearReplyTos ();
66+ $ mail ->clearAttachments ();
67+ } else {
68+ $ mail = new PHPMailer ();
69+ $ mail ->isSMTP ();
70+ $ mail ->Host = $ this ->host ;
71+ $ mail ->Port = $ this ->port ;
72+ $ mail ->SMTPAuth = !empty ($ this ->username ) && !empty ($ this ->password );
73+ $ mail ->Username = $ this ->username ;
74+ $ mail ->Password = $ this ->password ;
75+ $ mail ->SMTPSecure = $ this ->smtpSecure ;
76+ $ mail ->SMTPAutoTLS = $ this ->smtpAutoTLS ;
77+ $ mail ->Timeout = $ this ->timeout ;
78+ $ mail ->SMTPKeepAlive = $ this ->keepAlive ;
79+
80+ if ($ this ->keepAlive ) {
81+ $ this ->mail = $ mail ;
82+ }
83+ }
84+
5785 $ mail ->XMailer = $ this ->xMailer ;
58- $ mail ->Host = $ this ->host ;
59- $ mail ->Port = $ this ->port ;
60- $ mail ->SMTPAuth = !empty ($ this ->username ) && !empty ($ this ->password );
61- $ mail ->Username = $ this ->username ;
62- $ mail ->Password = $ this ->password ;
63- $ mail ->SMTPSecure = $ this ->smtpSecure ;
64- $ mail ->SMTPAutoTLS = $ this ->smtpAutoTLS ;
65- $ mail ->Timeout = $ this ->timeout ;
6686 $ mail ->CharSet = 'UTF-8 ' ;
87+ $ mail ->getSMTPInstance ()->Timelimit = $ this ->timelimit ;
6788 $ mail ->Subject = $ message ->getSubject ();
6889 $ mail ->Body = $ message ->getContent ();
6990 $ mail ->setFrom ($ message ->getFromEmail (), $ message ->getFromName ());
@@ -95,19 +116,33 @@ protected function process(EmailMessage $message): array
95116 $ size = 0 ;
96117
97118 foreach ($ message ->getAttachments () as $ attachment ) {
98- $ size += \filesize ($ attachment ->getPath ());
119+ if ($ attachment ->getContent () !== null ) {
120+ $ size += \strlen ($ attachment ->getContent ());
121+ } else {
122+ $ size += \filesize ($ attachment ->getPath ());
123+ }
99124 }
100125
101126 if ($ size > self ::MAX_ATTACHMENT_BYTES ) {
102127 throw new \Exception ('Attachments size exceeds the maximum allowed size of 25MB ' );
103128 }
104129
105130 foreach ($ message ->getAttachments () as $ attachment ) {
106- $ mail ->addStringAttachment (
107- string: \file_get_contents ($ attachment ->getPath ()),
108- filename: $ attachment ->getName (),
109- type: $ attachment ->getType ()
110- );
131+ if ($ attachment ->getContent () !== null ) {
132+ $ mail ->addStringAttachment (
133+ string: $ attachment ->getContent (),
134+ filename: $ attachment ->getName (),
135+ encoding: PHPMailer::ENCODING_BASE64 ,
136+ type: $ attachment ->getType ()
137+ );
138+ } else {
139+ $ mail ->addStringAttachment (
140+ string: \file_get_contents ($ attachment ->getPath ()),
141+ filename: $ attachment ->getName (),
142+ encoding: PHPMailer::ENCODING_BASE64 ,
143+ type: $ attachment ->getType ()
144+ );
145+ }
111146 }
112147 }
113148
0 commit comments