|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +__all__ = ["AsyncSMTPClientWithResponse"] |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +import aiosmtplib |
| 9 | + |
| 10 | +from ..response import SMTPResponse |
| 11 | +from ...utils import sanitize_email |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from .aio_backend import AsyncSMTPBackend |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class AsyncSMTPClientWithResponse: |
| 20 | + """Async SMTP client built on aiosmtplib that returns SMTPResponse objects.""" |
| 21 | + |
| 22 | + def __init__(self, parent: AsyncSMTPBackend, **kwargs): |
| 23 | + self.parent = parent |
| 24 | + self.make_response = parent.make_response |
| 25 | + |
| 26 | + self.tls = kwargs.pop("tls", False) |
| 27 | + self.ssl = kwargs.pop("ssl", False) |
| 28 | + self.debug = kwargs.pop("debug", 0) |
| 29 | + if self.debug: |
| 30 | + logger.warning( |
| 31 | + "debug parameter is not supported in async mode; " |
| 32 | + "use Python logging instead" |
| 33 | + ) |
| 34 | + self.user = kwargs.pop("user", None) |
| 35 | + self.password = kwargs.pop("password", None) |
| 36 | + |
| 37 | + # aiosmtplib uses use_tls for implicit TLS (SMTPS) and |
| 38 | + # start_tls for STARTTLS after connect |
| 39 | + smtp_kwargs = dict(kwargs) |
| 40 | + smtp_kwargs["use_tls"] = self.ssl |
| 41 | + smtp_kwargs["start_tls"] = self.tls |
| 42 | + |
| 43 | + # aiosmtplib uses 'hostname' instead of 'host' |
| 44 | + if "host" in smtp_kwargs: |
| 45 | + smtp_kwargs["hostname"] = smtp_kwargs.pop("host") |
| 46 | + |
| 47 | + self._smtp = aiosmtplib.SMTP(**smtp_kwargs) |
| 48 | + self._esmtp = False |
| 49 | + |
| 50 | + async def initialize(self): |
| 51 | + await self._smtp.connect() |
| 52 | + # connect() may have already completed EHLO internally |
| 53 | + self._esmtp = self._smtp.supports_esmtp |
| 54 | + try: |
| 55 | + if self._smtp.is_ehlo_or_helo_needed: |
| 56 | + try: |
| 57 | + await self._smtp.ehlo() |
| 58 | + self._esmtp = True |
| 59 | + except aiosmtplib.SMTPHeloError: |
| 60 | + # aiosmtplib closes the transport on 421 responses |
| 61 | + # before raising; don't attempt HELO on a dead connection |
| 62 | + if not self._smtp.is_connected: |
| 63 | + raise |
| 64 | + await self._smtp.helo() |
| 65 | + # aiosmtplib sets supports_esmtp before checking the |
| 66 | + # response code, so it may be True even after EHLO |
| 67 | + # failed. Track the real state ourselves. |
| 68 | + self._esmtp = False |
| 69 | + if self.user: |
| 70 | + await self._smtp.login(self.user, self.password) |
| 71 | + except Exception: |
| 72 | + await self.quit() |
| 73 | + raise |
| 74 | + |
| 75 | + async def quit(self): |
| 76 | + """Closes the connection to the email server.""" |
| 77 | + try: |
| 78 | + await self._smtp.quit() |
| 79 | + except (aiosmtplib.SMTPServerDisconnected, ConnectionError): |
| 80 | + self._smtp.close() |
| 81 | + |
| 82 | + async def _rset(self): |
| 83 | + try: |
| 84 | + await self._smtp.rset() |
| 85 | + except (aiosmtplib.SMTPServerDisconnected, ConnectionError): |
| 86 | + pass |
| 87 | + |
| 88 | + async def sendmail( |
| 89 | + self, |
| 90 | + from_addr: str, |
| 91 | + to_addrs: list[str] | str, |
| 92 | + msg: bytes, |
| 93 | + mail_options: list[str] | None = None, |
| 94 | + rcpt_options: list[str] | None = None, |
| 95 | + ) -> SMTPResponse | None: |
| 96 | + |
| 97 | + if not to_addrs: |
| 98 | + return None |
| 99 | + |
| 100 | + rcpt_options = rcpt_options or [] |
| 101 | + mail_options = mail_options or [] |
| 102 | + esmtp_opts = [] |
| 103 | + if self._esmtp: |
| 104 | + if self._smtp.supports_extension("size"): |
| 105 | + esmtp_opts.append("size=%d" % len(msg)) |
| 106 | + for option in mail_options: |
| 107 | + esmtp_opts.append(option) |
| 108 | + |
| 109 | + response = self.make_response() |
| 110 | + |
| 111 | + from_addr = sanitize_email(from_addr) |
| 112 | + |
| 113 | + response.from_addr = from_addr |
| 114 | + response.esmtp_opts = esmtp_opts[:] |
| 115 | + |
| 116 | + try: |
| 117 | + resp = await self._smtp.mail(from_addr, options=esmtp_opts) |
| 118 | + except aiosmtplib.SMTPSenderRefused as exc: |
| 119 | + response.set_status( |
| 120 | + "mail", |
| 121 | + exc.code, |
| 122 | + exc.message.encode() if isinstance(exc.message, str) else exc.message, |
| 123 | + ) |
| 124 | + response.set_exception(exc) |
| 125 | + await self._rset() |
| 126 | + return response |
| 127 | + |
| 128 | + response.set_status( |
| 129 | + "mail", |
| 130 | + resp.code, |
| 131 | + resp.message.encode() if isinstance(resp.message, str) else resp.message, |
| 132 | + ) |
| 133 | + |
| 134 | + if resp.code != 250: |
| 135 | + await self._rset() |
| 136 | + response.set_exception( |
| 137 | + aiosmtplib.SMTPSenderRefused(resp.code, resp.message, from_addr) |
| 138 | + ) |
| 139 | + return response |
| 140 | + |
| 141 | + if not isinstance(to_addrs, (list, tuple)): |
| 142 | + to_addrs = [to_addrs] |
| 143 | + |
| 144 | + to_addrs = [sanitize_email(e) for e in to_addrs] |
| 145 | + |
| 146 | + response.to_addrs = to_addrs |
| 147 | + response.rcpt_options = rcpt_options[:] |
| 148 | + response.refused_recipients = {} |
| 149 | + |
| 150 | + for a in to_addrs: |
| 151 | + try: |
| 152 | + resp = await self._smtp.rcpt(a, options=rcpt_options) |
| 153 | + code = resp.code |
| 154 | + resp_msg = ( |
| 155 | + resp.message.encode() |
| 156 | + if isinstance(resp.message, str) |
| 157 | + else resp.message |
| 158 | + ) |
| 159 | + except aiosmtplib.SMTPRecipientRefused as exc: |
| 160 | + code = exc.code |
| 161 | + resp_msg = ( |
| 162 | + exc.message.encode() |
| 163 | + if isinstance(exc.message, str) |
| 164 | + else exc.message |
| 165 | + ) |
| 166 | + |
| 167 | + response.set_status("rcpt", code, resp_msg, recipient=a) |
| 168 | + if (code != 250) and (code != 251): |
| 169 | + response.refused_recipients[a] = (code, resp_msg) |
| 170 | + |
| 171 | + if len(response.refused_recipients) == len(to_addrs): |
| 172 | + await self._rset() |
| 173 | + refused_list = [ |
| 174 | + aiosmtplib.SMTPRecipientRefused( |
| 175 | + code, msg.decode() if isinstance(msg, bytes) else msg, addr |
| 176 | + ) |
| 177 | + for addr, (code, msg) in response.refused_recipients.items() |
| 178 | + ] |
| 179 | + response.set_exception(aiosmtplib.SMTPRecipientsRefused(refused_list)) |
| 180 | + return response |
| 181 | + |
| 182 | + try: |
| 183 | + resp = await self._smtp.data(msg) |
| 184 | + except aiosmtplib.SMTPDataError as exc: |
| 185 | + resp_msg = ( |
| 186 | + exc.message.encode() if isinstance(exc.message, str) else exc.message |
| 187 | + ) |
| 188 | + response.set_status("data", exc.code, resp_msg) |
| 189 | + response.set_exception(exc) |
| 190 | + await self._rset() |
| 191 | + return response |
| 192 | + |
| 193 | + resp_msg = ( |
| 194 | + resp.message.encode() if isinstance(resp.message, str) else resp.message |
| 195 | + ) |
| 196 | + response.set_status("data", resp.code, resp_msg) |
| 197 | + if resp.code != 250: |
| 198 | + await self._rset() |
| 199 | + response.set_exception(aiosmtplib.SMTPDataError(resp.code, resp.message)) |
| 200 | + return response |
| 201 | + |
| 202 | + response._finished = True |
| 203 | + return response |
0 commit comments