Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonmail.py
More file actions
Latest commit
86 lines (71 loc) · 3.13 KB
/
Copy pathpythonmail.py
File metadata and controls
86 lines (71 loc) · 3.13 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Send a test email via Gmail using an App-Specific Password.
Requirements:
- 2-Step Verification enabled on your Google account
- An App Password generated at: https://myaccount.google.com/apppasswords
- No extra packages needed (uses Python's built-in smtplib + email libs)
Usage:
1. Fill in the CONFIG section below (or set the matching environment variables).
2. Run: python send_gmail.py
"""
importos
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
# ─────────────────────────────────────────────
# CONFIG – edit here OR export as env vars
# ─────────────────────────────────────────────
SENDER_EMAIL=os.getenv("GMAIL_SENDER", "sender@gmail.com")
APP_PASSWORD=os.getenv("GMAIL_APP_PASS", "xxxx xxxx xxxx xxxx") # 16-char app password
RECIPIENT_EMAIL=os.getenv("GMAIL_TO", "recipient@example.com")
SUBJECT="Test email from Python smtplib"
BODY_TEXT="Hello! This is a plain-text test email sent via Gmail + an App Password."
BODY_HTML="""\
<html>
<body>
<p>Hello!<br>
This is an <b>HTML test email</b> sent via Gmail + an App Password.
</p>
</body>
</html>
"""
# ─────────────────────────────────────────────
defbuild_message(sender: str, recipient: str, subject: str,
body_text: str, body_html: str) ->MIMEMultipart:
"""Construct a MIME multipart/alternative message (plain + HTML)."""
msg=MIMEMultipart("alternative")
msg["Subject"] =subject
msg["From"] =sender
msg["To"] =recipient
# Plain text comes first; mail clients prefer the last part they understand,
# so HTML is attached second so capable clients render it.
msg.attach(MIMEText(body_text, "plain"))
msg.attach(MIMEText(body_html, "html"))
returnmsg
defsend_email(sender: str, app_password: str, recipient: str,
subject: str, body_text: str, body_html: str) ->None:
"""Send an email through Gmail's SMTP server using TLS."""
msg=build_message(sender, recipient, subject, body_text, body_html)
print(f"Connecting to smtp.gmail.com:587 …")
withsmtplib.SMTP("smtp.gmail.com", 587) asserver:
server.ehlo()
server.starttls() # upgrade connection to TLS
server.ehlo()
server.login(sender, app_password)
server.sendmail(sender, [recipient], msg.as_string())
print(f"✓ Email sent successfully to {recipient}")
if__name__=="__main__":
# Basic validation
if"xxxx"inAPP_PASSWORD:
raiseValueError(
"Replace APP_PASSWORD with a real 16-character App Password.\n"
"Generate one at: https://myaccount.google.com/apppasswords"
)
send_email(
sender=SENDER_EMAIL,
app_password=APP_PASSWORD,
recipient=RECIPIENT_EMAIL,
subject=SUBJECT,
body_text=BODY_TEXT,
body_html=BODY_HTML,
)