Skip to content

Commit

Permalink
vdk-core: Set sender when checking if email exists (#2376)
Browse files Browse the repository at this point in the history
Currently, when a target email address is checked if exists, we set a
dummy sender `"whatever"`. This works in cases, where the smtp server
does not enforce validation checks on the sender. It does not, however,
work in cases when the smtp server uses TLS (like Office365).

In such cases, the check of the target email address fails with Status
`503: Bad sequence of commands`, because the `.mail("whatever")` method
call sets the **FROM** field to *mail FROM:<whatever>*, and this causes
the smtp server to return `501 5.1.7 Invalid address`.

This change replces `"whatever"` with the actual sender address used to
send the email notifications (set through the `VDK_NOTIFICATION_SENDER`)
environment variable.

Testing Done:
Create a sample email script to first better understand the error, and
second to verify that the fix actually works.

Script is:
```python
HOST = "smtp.office365.com"
PORT = 587
SENDER = "<valid-address-as-set-by-VDK_NOTIFICATION_SENDER>"
USERNAME = "<sender-user-as-set-by-VDK_NOTIFICATION_SMTP_LOGIN_USERNAME>"
PASSWORD = "<password-as-set-by-VDK_NOTIFICATION_SMTP_LOGIN_PASSWORD>"
TARGET_ADDRESS = "<some-valid-email-address>"

s = smtplib.SMTP(HOST, PORT)
s.starttls()
s.login(USERNAME, PASSWORD)
s.set_debuglevel(1)

try:
    s.helo()
    # s.mail(SENDER)               # Test the actual fix
    s.mail("whatever")             # Check the full error
    resp = s.rcpt(TARGET_ADDRESS)
    print(resp)
except smtplib.SMTPServerDisconnected as e:
    print(f"SMTP connection error while checking if email address exists: {e}")
finally:
    s.quit()
```

Signed-off-by: Andon Andonov <[email protected]>
  • Loading branch information
doks5 authored Jul 11, 2023
1 parent b3c704b commit 08d442a
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _email_address_exists(self, email_address):
s = self.__smtp_server()
try:
s.helo()
s.mail("whatever")
s.mail(self._sender)
resp = s.rcpt(email_address)
return resp[0] == 250
except smtplib.SMTPServerDisconnected as e:
Expand Down

0 comments on commit 08d442a

Please sign in to comment.