Send email — Python Technical Interview Questions & Answers
Send email
Send email
- Send email sends mail with smtplib / email.message, or a Gmail API. You need host, port, login. Never hard-code passwords in source — use env vars.
- Classroom: show the Message structure (From, To, Subject, body). Real send needs credentials; the example prints the message text so it always runs.
- Send email — output: Marks then Asha scored 90. Real email.message object. SMTP send needs login — don’t put passwords in code.
Technical Interview Tip: Parts of an email + never commit passwords.
Code Examples for Send email
Example
# Send email
from email.message import EmailMessage
m = EmailMessage()
m["From"] = "teacher@school.in"
m["To"] = "asha@school.in"
m["Subject"] = "Marks"
m.set_content("Asha scored 90")
print(m["Subject"])
print(m.get_content().strip())
Send email — output: Marks then Asha scored 90. Real email.message object. SMTP send needs login — don’t put passwords in code.
How to Explain Send email to an Interviewer
Explain Send email as if you are teaching a junior — definition, then one tiny script.
From / To / Subject / body smtp or Gmail API
What does the example print, and why?
Send email — output: Marks then Asha scored 90
What mistake do freshers make with Send email?
Password in code.