| 1 |
""" |
| 2 |
SMTP.sendmail and SMTP.send_message method testing. |
| 3 |
""" |
| 4 |
|
| 5 |
import copy |
| 6 |
import email.generator |
| 7 |
import email.header |
| 8 |
import email.message |
| 9 |
from typing import Any |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from aiosmtplib import ( |
| 14 |
SMTP, |
| 15 |
SMTPNotSupported, |
| 16 |
SMTPRecipientsRefused, |
| 17 |
SMTPResponseException, |
| 18 |
SMTPStatus, |
| 19 |
) |
| 20 |
|
| 21 |
from .smtpd import ( |
| 22 |
mock_response_done, |
| 23 |
mock_response_error_disconnect, |
| 24 |
mock_response_bad_command_sequence, |
| 25 |
mock_response_mailbox_unavailable, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
async def test_sendmail_simple_success( |
| 30 |
smtp_client: SMTP, |
| 31 |
sender_str: str, |
| 32 |
recipient_str: str, |
| 33 |
message_str: str, |
| 34 |
) -> None: |
| 35 |
async with smtp_client: |
| 36 |
errors, response = await smtp_client.sendmail( |
| 37 |
sender_str, [recipient_str], message_str |
| 38 |
) |
| 39 |
|
| 40 |
assert not errors |
| 41 |
assert isinstance(errors, dict) |
| 42 |
assert response != "" |
| 43 |
|
| 44 |
|
| 45 |
async def test_sendmail_binary_content( |
| 46 |
smtp_client: SMTP, |
| 47 |
sender_str: str, |
| 48 |
recipient_str: str, |
| 49 |
message_str: str, |
| 50 |
) -> None: |
| 51 |
async with smtp_client: |
| 52 |
errors, response = await smtp_client.sendmail( |
| 53 |
sender_str, [recipient_str], bytes(message_str, "ascii") |
| 54 |
) |
| 55 |
|
| 56 |
assert not errors |
| 57 |
assert isinstance(errors, dict) |
| 58 |
assert response != "" |
| 59 |
|
| 60 |
|
| 61 |
async def test_sendmail_with_recipients_string( |
| 62 |
smtp_client: SMTP, |
| 63 |
sender_str: str, |
| 64 |
recipient_str: str, |
| 65 |
message_str: str, |
| 66 |
) -> None: |
| 67 |
async with smtp_client: |
| 68 |
errors, response = await smtp_client.sendmail( |
| 69 |
sender_str, recipient_str, message_str |
| 70 |
) |
| 71 |
|
| 72 |
assert not errors |
| 73 |
assert response != "" |
| 74 |
|
| 75 |
|
| 76 |
async def test_sendmail_with_mail_option( |
| 77 |
smtp_client: SMTP, |
| 78 |
sender_str: str, |
| 79 |
recipient_str: str, |
| 80 |
message_str: str, |
| 81 |
) -> None: |
| 82 |
async with smtp_client: |
| 83 |
errors, response = await smtp_client.sendmail( |
| 84 |
sender_str, [recipient_str], message_str, mail_options=["BODY=8BITMIME"] |
| 85 |
) |
| 86 |
|
| 87 |
assert not errors |
| 88 |
assert response != "" |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_done) |
| 92 |
async def test_sendmail_without_size_option( |
| 93 |
smtp_client: SMTP, |
| 94 |
sender_str: str, |
| 95 |
recipient_str: str, |
| 96 |
message_str: str, |
| 97 |
) -> None: |
| 98 |
async with smtp_client: |
| 99 |
errors, response = await smtp_client.sendmail( |
| 100 |
sender_str, [recipient_str], message_str |
| 101 |
) |
| 102 |
|
| 103 |
assert not errors |
| 104 |
assert response != "" |
| 105 |
|
| 106 |
|
| 107 |
async def test_sendmail_with_invalid_mail_option( |
| 108 |
smtp_client: SMTP, |
| 109 |
sender_str: str, |
| 110 |
recipient_str: str, |
| 111 |
message_str: str, |
| 112 |
) -> None: |
| 113 |
async with smtp_client: |
| 114 |
with pytest.raises(SMTPResponseException) as excinfo: |
| 115 |
await smtp_client.sendmail( |
| 116 |
sender_str, |
| 117 |
[recipient_str], |
| 118 |
message_str, |
| 119 |
mail_options=["BADDATA=0x00000000"], |
| 120 |
) |
| 121 |
|
| 122 |
assert excinfo.value.code == SMTPStatus.syntax_error |
| 123 |
|
| 124 |
|
| 125 |
async def test_sendmail_with_rcpt_option( |
| 126 |
smtp_client: SMTP, |
| 127 |
sender_str: str, |
| 128 |
recipient_str: str, |
| 129 |
message_str: str, |
| 130 |
) -> None: |
| 131 |
async with smtp_client: |
| 132 |
with pytest.raises(SMTPRecipientsRefused) as excinfo: |
| 133 |
await smtp_client.sendmail( |
| 134 |
sender_str, |
| 135 |
[recipient_str], |
| 136 |
message_str, |
| 137 |
rcpt_options=["NOTIFY=FAILURE,DELAY"], |
| 138 |
) |
| 139 |
|
| 140 |
recipient_exc = excinfo.value.recipients[0] |
| 141 |
assert recipient_exc.code == SMTPStatus.syntax_error |
| 142 |
assert ( |
| 143 |
recipient_exc.message |
| 144 |
== "RCPT TO parameters not recognized or not implemented" |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
async def test_sendmail_simple_failure(smtp_client: SMTP) -> None: |
| 149 |
async with smtp_client: |
| 150 |
with pytest.raises(SMTPRecipientsRefused): |
| 151 |
# @@ is an invalid recipient. |
| 152 |
await smtp_client.sendmail("[email protected]", ["@@"], "blah") |
| 153 |
|
| 154 |
|
| 155 |
async def test_sendmail_smtputf8_not_supported(smtp_client: SMTP) -> None: |
| 156 |
async with smtp_client: |
| 157 |
with pytest.raises(SMTPNotSupported, match="SMTPUTF8 is not supported"): |
| 158 |
await smtp_client.sendmail( |
| 159 |
"[email protected]", |
| 160 |
["bø[email protected]"], |
| 161 |
"blah", |
| 162 |
mail_options=["SMTPUTF8"], |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.smtpd_mocks(smtp_DATA=mock_response_error_disconnect) |
| 167 |
async def test_sendmail_error_silent_rset_handles_disconnect( |
| 168 |
smtp_client: SMTP, |
| 169 |
sender_str: str, |
| 170 |
recipient_str: str, |
| 171 |
message_str: str, |
| 172 |
) -> None: |
| 173 |
async with smtp_client: |
| 174 |
with pytest.raises(SMTPResponseException): |
| 175 |
await smtp_client.sendmail(sender_str, [recipient_str], message_str) |
| 176 |
|
| 177 |
|
| 178 |
@pytest.mark.smtpd_mocks(smtp_MAIL=mock_response_mailbox_unavailable) |
| 179 |
async def test_rset_after_sendmail_error_response_to_mail( |
| 180 |
smtp_client: SMTP, |
| 181 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 182 |
) -> None: |
| 183 |
""" |
| 184 |
If an error response is given to the MAIL command in the sendmail method, |
| 185 |
test that we reset the server session. |
| 186 |
""" |
| 187 |
async with smtp_client: |
| 188 |
response = await smtp_client.ehlo() |
| 189 |
assert response.code == SMTPStatus.completed |
| 190 |
|
| 191 |
with pytest.raises(SMTPResponseException) as excinfo: |
| 192 |
await smtp_client.sendmail( |
| 193 |
"[email protected]", ["[email protected]"], "Hello World" |
| 194 |
) |
| 195 |
|
| 196 |
assert excinfo.value.code == SMTPStatus.mailbox_unavailable |
| 197 |
assert received_commands[-1][0] == "RSET" |
| 198 |
|
| 199 |
|
| 200 |
@pytest.mark.smtpd_mocks(smtp_RCPT=mock_response_mailbox_unavailable) |
| 201 |
async def test_rset_after_sendmail_error_response_to_rcpt( |
| 202 |
smtp_client: SMTP, |
| 203 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 204 |
) -> None: |
| 205 |
""" |
| 206 |
If an error response is given to the RCPT command in the sendmail method, |
| 207 |
test that we reset the server session. |
| 208 |
""" |
| 209 |
async with smtp_client: |
| 210 |
response = await smtp_client.ehlo() |
| 211 |
assert response.code == SMTPStatus.completed |
| 212 |
|
| 213 |
with pytest.raises(SMTPRecipientsRefused) as excinfo: |
| 214 |
await smtp_client.sendmail( |
| 215 |
"[email protected]", ["[email protected]"], "Hello World" |
| 216 |
) |
| 217 |
|
| 218 |
assert excinfo.value.recipients[0].code == SMTPStatus.mailbox_unavailable |
| 219 |
assert received_commands[-1][0] == "RSET" |
| 220 |
|
| 221 |
|
| 222 |
@pytest.mark.smtpd_mocks(smtp_DATA=mock_response_bad_command_sequence) |
| 223 |
async def test_rset_after_sendmail_error_response_to_data( |
| 224 |
smtp_client: SMTP, |
| 225 |
sender_str: str, |
| 226 |
recipient_str: str, |
| 227 |
message_str: str, |
| 228 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 229 |
) -> None: |
| 230 |
""" |
| 231 |
If an error response is given to the DATA command in the sendmail method, |
| 232 |
test that we reset the server session. |
| 233 |
""" |
| 234 |
async with smtp_client: |
| 235 |
response = await smtp_client.ehlo() |
| 236 |
assert response.code == SMTPStatus.completed |
| 237 |
|
| 238 |
with pytest.raises(SMTPResponseException) as excinfo: |
| 239 |
await smtp_client.sendmail(sender_str, [recipient_str], message_str) |
| 240 |
|
| 241 |
assert excinfo.value.code == SMTPStatus.bad_command_sequence |
| 242 |
assert received_commands[-1][0] == "RSET" |
| 243 |
|
| 244 |
|
| 245 |
async def test_send_message(smtp_client: SMTP, message: email.message.Message) -> None: |
| 246 |
async with smtp_client: |
| 247 |
errors, response = await smtp_client.send_message(message) |
| 248 |
|
| 249 |
assert not errors |
| 250 |
assert isinstance(errors, dict) |
| 251 |
assert response != "" |
| 252 |
|
| 253 |
|
| 254 |
async def test_send_message_with_sender_and_recipient_args( |
| 255 |
smtp_client: SMTP, |
| 256 |
message: email.message.EmailMessage, |
| 257 |
received_messages: list[email.message.EmailMessage], |
| 258 |
) -> None: |
| 259 |
sender = "[email protected]" |
| 260 |
recipients = ["[email protected]", "[email protected]"] |
| 261 |
async with smtp_client: |
| 262 |
errors, response = await smtp_client.send_message( |
| 263 |
message, sender=sender, recipients=recipients |
| 264 |
) |
| 265 |
|
| 266 |
assert not errors |
| 267 |
assert isinstance(errors, dict) |
| 268 |
assert response != "" |
| 269 |
|
| 270 |
assert len(received_messages) == 1 |
| 271 |
assert received_messages[0]["X-MailFrom"] == sender |
| 272 |
assert received_messages[0]["X-RcptTo"] == ", ".join(recipients) |
| 273 |
|
| 274 |
|
| 275 |
async def test_send_message_with_cc_recipients( |
| 276 |
smtp_client: SMTP, |
| 277 |
recipient_str: str, |
| 278 |
message: email.message.EmailMessage, |
| 279 |
received_messages: list[email.message.EmailMessage], |
| 280 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 281 |
) -> None: |
| 282 |
cc_recipients = ["[email protected]", "[email protected]"] |
| 283 |
message["Cc"] = ", ".join(cc_recipients) |
| 284 |
|
| 285 |
async with smtp_client: |
| 286 |
errors, _ = await smtp_client.send_message(message) |
| 287 |
|
| 288 |
assert not errors |
| 289 |
|
| 290 |
assert len(received_messages) == 1 |
| 291 |
assert ( |
| 292 |
received_messages[0]["X-RcptTo"] |
| 293 |
== f"{recipient_str}, {', '.join(cc_recipients)}" |
| 294 |
) |
| 295 |
|
| 296 |
assert received_commands[2][0] == "RCPT" |
| 297 |
assert received_commands[2][1][0] == recipient_str |
| 298 |
assert received_commands[3][0] == "RCPT" |
| 299 |
assert received_commands[3][1][0] == cc_recipients[0] |
| 300 |
assert received_commands[4][0] == "RCPT" |
| 301 |
assert received_commands[4][1][0] == cc_recipients[1] |
| 302 |
|
| 303 |
|
| 304 |
async def test_send_message_with_bcc_recipients( |
| 305 |
smtp_client: SMTP, |
| 306 |
recipient_str: str, |
| 307 |
message: email.message.EmailMessage, |
| 308 |
received_messages: list[email.message.EmailMessage], |
| 309 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 310 |
) -> None: |
| 311 |
bcc_recipients = ["[email protected]", "[email protected]"] |
| 312 |
message["Bcc"] = ", ".join(bcc_recipients) |
| 313 |
|
| 314 |
async with smtp_client: |
| 315 |
errors, _ = await smtp_client.send_message(message) |
| 316 |
|
| 317 |
assert not errors |
| 318 |
|
| 319 |
assert len(received_messages) == 1 |
| 320 |
|
| 321 |
assert received_commands[2][0] == "RCPT" |
| 322 |
assert received_commands[2][1][0] == recipient_str |
| 323 |
assert received_commands[3][0] == "RCPT" |
| 324 |
assert received_commands[3][1][0] == bcc_recipients[0] |
| 325 |
assert received_commands[4][0] == "RCPT" |
| 326 |
assert received_commands[4][1][0] == bcc_recipients[1] |
| 327 |
|
| 328 |
|
| 329 |
async def test_send_message_with_cc_and_bcc_recipients( |
| 330 |
smtp_client: SMTP, |
| 331 |
recipient_str: str, |
| 332 |
message: email.message.EmailMessage, |
| 333 |
received_messages: list[email.message.EmailMessage], |
| 334 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 335 |
) -> None: |
| 336 |
cc_recipient = "[email protected]" |
| 337 |
message["Cc"] = cc_recipient |
| 338 |
bcc_recipient = "[email protected]" |
| 339 |
message["Bcc"] = bcc_recipient |
| 340 |
|
| 341 |
async with smtp_client: |
| 342 |
errors, _ = await smtp_client.send_message(message) |
| 343 |
|
| 344 |
assert not errors |
| 345 |
|
| 346 |
assert len(received_messages) == 1 |
| 347 |
assert received_messages[0]["To"] == recipient_str |
| 348 |
assert received_messages[0]["Cc"] == cc_recipient |
| 349 |
# BCC shouldn't be passed through |
| 350 |
assert received_messages[0]["Bcc"] is None |
| 351 |
|
| 352 |
assert received_commands[2][0] == "RCPT" |
| 353 |
assert received_commands[2][1][0] == recipient_str |
| 354 |
assert received_commands[3][0] == "RCPT" |
| 355 |
assert received_commands[3][1][0] == cc_recipient |
| 356 |
assert received_commands[4][0] == "RCPT" |
| 357 |
assert received_commands[4][1][0] == bcc_recipient |
| 358 |
|
| 359 |
|
| 360 |
async def test_send_message_recipient_str( |
| 361 |
smtp_client: SMTP, |
| 362 |
message: email.message.EmailMessage, |
| 363 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 364 |
) -> None: |
| 365 |
recipient_str = "[email protected]" |
| 366 |
async with smtp_client: |
| 367 |
errors, response = await smtp_client.send_message( |
| 368 |
message, recipients=recipient_str |
| 369 |
) |
| 370 |
|
| 371 |
assert not errors |
| 372 |
assert isinstance(errors, dict) |
| 373 |
assert response != "" |
| 374 |
assert received_commands[2][1][0] == recipient_str |
| 375 |
|
| 376 |
|
| 377 |
async def test_send_message_mail_options( |
| 378 |
smtp_client: SMTP, |
| 379 |
message: email.message.EmailMessage, |
| 380 |
) -> None: |
| 381 |
async with smtp_client: |
| 382 |
errors, response = await smtp_client.send_message( |
| 383 |
message, mail_options=["BODY=8BITMIME"] |
| 384 |
) |
| 385 |
|
| 386 |
assert not errors |
| 387 |
assert isinstance(errors, dict) |
| 388 |
assert response != "" |
| 389 |
|
| 390 |
|
| 391 |
async def test_send_multiple_messages_in_sequence( |
| 392 |
smtp_client: SMTP, message: email.message.EmailMessage |
| 393 |
) -> None: |
| 394 |
message1 = copy.copy(message) |
| 395 |
|
| 396 |
message2 = copy.copy(message) |
| 397 |
del message2["To"] |
| 398 |
message2["To"] = "[email protected]" |
| 399 |
|
| 400 |
async with smtp_client: |
| 401 |
errors1, response1 = await smtp_client.send_message(message1) |
| 402 |
|
| 403 |
assert not errors1 |
| 404 |
assert isinstance(errors1, dict) |
| 405 |
assert response1 != "" |
| 406 |
|
| 407 |
errors2, response2 = await smtp_client.send_message(message2) |
| 408 |
|
| 409 |
assert not errors2 |
| 410 |
assert isinstance(errors2, dict) |
| 411 |
assert response2 != "" |
| 412 |
|
| 413 |
|
| 414 |
async def test_send_message_without_recipients( |
| 415 |
smtp_client: SMTP, message: email.message.EmailMessage |
| 416 |
) -> None: |
| 417 |
del message["To"] |
| 418 |
|
| 419 |
async with smtp_client: |
| 420 |
with pytest.raises(ValueError): |
| 421 |
await smtp_client.send_message(message) |
| 422 |
|
| 423 |
|
| 424 |
async def test_send_message_without_sender( |
| 425 |
smtp_client: SMTP, message: email.message.EmailMessage |
| 426 |
) -> None: |
| 427 |
del message["From"] |
| 428 |
|
| 429 |
async with smtp_client: |
| 430 |
with pytest.raises(ValueError): |
| 431 |
await smtp_client.send_message(message) |
| 432 |
|
| 433 |
|
| 434 |
@pytest.mark.parametrize( |
| 435 |
"message", ["message", "compat32_message", "mime_message"], indirect=True |
| 436 |
) |
| 437 |
@pytest.mark.smtpd_options(smtputf8=True) |
| 438 |
async def test_send_message_smtputf8_sender( |
| 439 |
smtp_client: SMTP, |
| 440 |
message: email.message.EmailMessage | email.message.Message, |
| 441 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 442 |
received_messages: list[email.message.EmailMessage], |
| 443 |
) -> None: |
| 444 |
del message["From"] |
| 445 |
message["From"] = "séndër@exåmple.com" |
| 446 |
|
| 447 |
async with smtp_client: |
| 448 |
errors, response = await smtp_client.send_message(message) |
| 449 |
|
| 450 |
assert not errors |
| 451 |
assert response != "" |
| 452 |
|
| 453 |
assert received_commands[1][0] == "MAIL" |
| 454 |
assert received_commands[1][1][0] == message["From"] |
| 455 |
# Size varies depending on the message type |
| 456 |
assert received_commands[1][1][1][0].startswith("SIZE=") |
| 457 |
assert received_commands[1][1][1][1:] == ["SMTPUTF8", "BODY=8BITMIME"] |
| 458 |
|
| 459 |
assert len(received_messages) == 1 |
| 460 |
assert received_messages[0]["X-MailFrom"] == message["From"] |
| 461 |
|
| 462 |
|
| 463 |
@pytest.mark.smtpd_options(smtputf8=True) |
| 464 |
@pytest.mark.parametrize( |
| 465 |
"mail_options", |
| 466 |
(None, ["SMTPUTF8"]), |
| 467 |
ids=("no_mail_options", "smtputf8_option"), |
| 468 |
) |
| 469 |
async def test_send_mime_message_smtputf8_recipient( |
| 470 |
smtp_client: SMTP, |
| 471 |
mime_message: email.message.EmailMessage, |
| 472 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 473 |
received_messages: list[email.message.EmailMessage], |
| 474 |
mail_options: list[str] | None, |
| 475 |
) -> None: |
| 476 |
mime_message["To"] = "reçipïént@exåmple.com" |
| 477 |
|
| 478 |
async with smtp_client: |
| 479 |
errors, response = await smtp_client.send_message( |
| 480 |
mime_message, mail_options=mail_options |
| 481 |
) |
| 482 |
|
| 483 |
assert not errors |
| 484 |
assert response != "" |
| 485 |
|
| 486 |
assert received_commands[2][0] == "RCPT" |
| 487 |
assert received_commands[2][1][0] == mime_message["To"] |
| 488 |
|
| 489 |
assert len(received_messages) == 1 |
| 490 |
assert received_messages[0]["X-RcptTo"] == ", ".join(mime_message.get_all("To")) |
| 491 |
|
| 492 |
|
| 493 |
@pytest.mark.smtpd_options(smtputf8=True) |
| 494 |
async def test_send_compat32_message_smtputf8_recipient( |
| 495 |
smtp_client: SMTP, |
| 496 |
compat32_message: email.message.Message, |
| 497 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 498 |
received_messages: list[email.message.EmailMessage], |
| 499 |
) -> None: |
| 500 |
recipient_bytes = bytes("reçipïént@exåmple.com", "utf-8") |
| 501 |
compat32_message["To"] = email.header.Header(recipient_bytes, "utf-8") |
| 502 |
|
| 503 |
async with smtp_client: |
| 504 |
errors, response = await smtp_client.send_message(compat32_message) |
| 505 |
|
| 506 |
assert not errors |
| 507 |
assert response != "" |
| 508 |
|
| 509 |
assert received_commands[2][0] == "RCPT" |
| 510 |
assert received_commands[2][1][0] == compat32_message["To"] |
| 511 |
|
| 512 |
assert len(received_messages) == 1 |
| 513 |
assert ( |
| 514 |
received_messages[0]["X-RcptTo"] |
| 515 |
== "[email protected], reçipïént@exåmple.com" |
| 516 |
) |
| 517 |
|
| 518 |
|
| 519 |
@pytest.mark.smtpd_options(smtputf8=False) |
| 520 |
async def test_send_message_smtputf8_not_supported( |
| 521 |
smtp_client: SMTP, message: email.message.EmailMessage |
| 522 |
) -> None: |
| 523 |
del message["To"] |
| 524 |
message["To"] = "reçipïént2@exåmple.com" |
| 525 |
|
| 526 |
async with smtp_client: |
| 527 |
with pytest.raises(SMTPNotSupported): |
| 528 |
await smtp_client.send_message(message) |
| 529 |
|
| 530 |
|
| 531 |
@pytest.mark.smtpd_options(smtputf8=False) |
| 532 |
async def test_send_compat32_message_utf8_text_without_smtputf8( |
| 533 |
smtp_client: SMTP, |
| 534 |
compat32_message: email.message.Message, |
| 535 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 536 |
received_messages: list[email.message.EmailMessage], |
| 537 |
) -> None: |
| 538 |
compat32_message["To"] = email.header.Header( |
| 539 |
"reçipïént <[email protected]>", "utf-8" |
| 540 |
) |
| 541 |
|
| 542 |
async with smtp_client: |
| 543 |
errors, response = await smtp_client.send_message(compat32_message) |
| 544 |
|
| 545 |
assert not errors |
| 546 |
assert response != "" |
| 547 |
|
| 548 |
assert received_commands[2][0] == "RCPT" |
| 549 |
assert received_commands[2][1][0] == compat32_message["To"].encode() |
| 550 |
|
| 551 |
assert len(received_messages) == 1 |
| 552 |
assert ( |
| 553 |
received_messages[0]["X-RcptTo"] |
| 554 |
== "[email protected], [email protected]" |
| 555 |
) |
| 556 |
# Name should be encoded |
| 557 |
assert received_messages[0].get_all("To") == [ |
| 558 |
"[email protected]", |
| 559 |
"=?utf-8?b?cmXDp2lww6/DqW50IDxyZWNpcGllbnQyQGV4YW1wbGUuY29tPg==?=", |
| 560 |
] |
| 561 |
|
| 562 |
|
| 563 |
@pytest.mark.smtpd_options(smtputf8=False) |
| 564 |
async def test_send_mime_message_utf8_text_without_smtputf8( |
| 565 |
smtp_client: SMTP, |
| 566 |
mime_message: email.message.EmailMessage, |
| 567 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 568 |
received_messages: list[email.message.EmailMessage], |
| 569 |
) -> None: |
| 570 |
mime_message["To"] = "reçipïént <[email protected]>" |
| 571 |
|
| 572 |
async with smtp_client: |
| 573 |
errors, response = await smtp_client.send_message(mime_message) |
| 574 |
|
| 575 |
assert not errors |
| 576 |
assert response != "" |
| 577 |
|
| 578 |
assert received_commands[2][0] == "RCPT" |
| 579 |
assert received_commands[2][1][0] == mime_message["To"] |
| 580 |
|
| 581 |
assert len(received_messages) == 1 |
| 582 |
assert ( |
| 583 |
received_messages[0]["X-RcptTo"] |
| 584 |
== "[email protected], [email protected]" |
| 585 |
) |
| 586 |
# Name should be encoded |
| 587 |
assert received_messages[0].get_all("To") == [ |
| 588 |
"[email protected]", |
| 589 |
"=?utf-8?b?cmXDp2lww6/DqW50IDxyZWNpcGllbnQyQGV4YW1wbGUuY29tPg==?=", |
| 590 |
] |
| 591 |
|
| 592 |
|
| 593 |
@pytest.mark.parametrize( |
| 594 |
"message", ["message", "compat32_message", "mime_message"], indirect=True |
| 595 |
) |
| 596 |
@pytest.mark.smtpd_options(**{"smtputf8": False, "7bit": True}) |
| 597 |
async def test_send_message_7bit( |
| 598 |
smtp_client: SMTP, |
| 599 |
message: email.message.EmailMessage | email.message.Message, |
| 600 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 601 |
) -> None: |
| 602 |
async with smtp_client: |
| 603 |
errors, response = await smtp_client.send_message(message) |
| 604 |
|
| 605 |
assert not errors |
| 606 |
assert response != "" |
| 607 |
|
| 608 |
assert "BODY=8BITMIME" not in received_commands[1][1][1] |
| 609 |
|
| 610 |
|
| 611 |
async def test_sendmail_empty_sender( |
| 612 |
smtp_client: SMTP, recipient_str: str, message_str: str |
| 613 |
) -> None: |
| 614 |
async with smtp_client: |
| 615 |
errors, response = await smtp_client.sendmail("", [recipient_str], message_str) |
| 616 |
|
| 617 |
assert not errors |
| 618 |
assert isinstance(errors, dict) |
| 619 |
assert response != "" |
| 620 |
|
| 621 |
|
| 622 |
async def test_sendmail_size_option_counts_transmitted_octets_bytes( |
| 623 |
smtp_client: SMTP, |
| 624 |
sender_str: str, |
| 625 |
recipient_str: str, |
| 626 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 627 |
) -> None: |
| 628 |
# Bare LF line endings are converted to CRLF on the wire, the body has |
| 629 |
# multibyte UTF-8, and there is no trailing newline (one gets added). |
| 630 |
message = b"From: [email protected]\nTo: [email protected]\n\nH\xc3\xa9llo\n.dot" |
| 631 |
expected_size = len( |
| 632 |
b"From: [email protected]\r\nTo: [email protected]\r\n\r\nH\xc3\xa9llo\r\n.dot\r\n" |
| 633 |
) |
| 634 |
|
| 635 |
async with smtp_client: |
| 636 |
await smtp_client.sendmail(sender_str, [recipient_str], message) |
| 637 |
|
| 638 |
assert received_commands[1][0] == "MAIL" |
| 639 |
assert received_commands[1][1][1][0] == f"SIZE={expected_size}" |
| 640 |
|
| 641 |
|
| 642 |
async def test_sendmail_size_option_counts_transmitted_octets_str( |
| 643 |
smtp_client: SMTP, |
| 644 |
sender_str: str, |
| 645 |
recipient_str: str, |
| 646 |
received_commands: list[tuple[str, tuple[Any, ...]]], |
| 647 |
) -> None: |
| 648 |
message = "From: [email protected]\nTo: [email protected]\n\nHello\n" |
| 649 |
expected_size = len(b"From: [email protected]\r\nTo: [email protected]\r\n\r\nHello\r\n") |
| 650 |
|
| 651 |
async with smtp_client: |
| 652 |
await smtp_client.sendmail(sender_str, [recipient_str], message) |
| 653 |
|
| 654 |
assert received_commands[1][0] == "MAIL" |
| 655 |
assert received_commands[1][1][1][0] == f"SIZE={expected_size}" |