-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path02-smtplib-auth.py
More file actions
68 lines (55 loc) · 1.96 KB
/
02-smtplib-auth.py
File metadata and controls
68 lines (55 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# Code credit to PyMoTW3
# This is not an exact copy, and was only used as a
# study aid in the process of understanding `smtplib`
# and its use.
import sys
import smtplib
import email.utils
import getpass
from email.mime.text import MIMEText
recipient = input("Recipient address: ")
server_name = input("Mail Server Name: ")
try:
server_port = int(input("Mail Server Port: "))
except ValueError:
sys.exit("Expecting an integer as Port number!")
use_tls = input("Use TLS? [Y/N]").lower()
username = input("Username: ")
# Get the password without echoing
password = getpass.getpass(f"Password for {username}: ")
message = MIMEText("Hello, how are you?")
message.set_unixfrom("Author")
message["To"] = email.utils.format(("Recipient", "recipient"))
message["From"] = email.utils.format(("Author", "arvimal@yahoo.in"))
message["Subject"] = "Test mail"
if use_tls == "y":
print("Setting up a secure connection.")
else:
print("TLS not activated, connection insecure!")
mail_server = smtplib.SMTP(server_name, server_port)
# Activate the debuglevel
mail_server.set_debuglevel(True)
# The EHLO flag send from the client, will prompt the
# server to send back the features it support, to the
# client.
mail_server.ehlo()
# The `has_extn()` method can be used to list the features
# the server supports. Here, if the server supports TLS, we
# try to use it.
if mail_server.has_extn("STARTTLS"):
print("Connecting using TLS.")
mail_server.starttls()
# We need to use the `EHLO` method once again,
# to identify ourself for the TLS session
mail_server.ehlo()
else:
print(f"{server_name} does not support TLS")
if mail_server.has_extn("AUTH"):
print("Trying to authenticate using username/password")
mail_server.login(username, password)
else:
print(f"{server_name} does not support authentication")
mail_server.sendmail("arvimal@yahoo.in", [recipient], message.as_string())
# Quit once the message has been send
mail_server.quit()