Appearance
Sending Email with SMTP
SMTP is the protocol used to send emails, and Python provides built-in support through the smtplib
and email
modules. The email
module constructs the emails, while smtplib
handles the sending.
Sending a Simple Text Email
To create a simple plain text email:
python
from email.mime.text import MIMEText
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
Next, send the email:
python
import smtplib
from_addr = input('From: ')
password = input('Password: ')
to_addr = input('To: ')
smtp_server = input('SMTP server: ')
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
Adding Email Headers
To ensure a complete email, add headers like From
, To
, and Subject
:
python
from email import encoders
from email.header import Header
from email.utils import parseaddr, formataddr
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
msg['From'] = _format_addr('Python Lover <%s>' % from_addr)
msg['To'] = _format_addr('Admin <%s>' % to_addr)
msg['Subject'] = Header('Greetings from SMTP...', 'utf-8').encode()
Sending HTML Emails
To send an HTML email, simply change the MIME type:
python
msg = MIMEText('<html><body><h1>Hello</h1><p>send by <a href="http://www.python.org">Python</a>...</p></body></html>', 'html', 'utf-8')
Sending Emails with Attachments
To include attachments, use MIMEMultipart
:
python
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
msg = MIMEMultipart()
msg['From'] = _format_addr('Python Lover <%s>' % from_addr)
msg['To'] = _format_addr('Admin <%s>' % to_addr)
msg['Subject'] = Header('Greetings with Attachment...', 'utf-8').encode()
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
with open('path/to/file.png', 'rb') as f:
mime = MIMEBase('image', 'png', filename='file.png')
mime.add_header('Content-Disposition', 'attachment', filename='file.png')
mime.set_payload(f.read())
encoders.encode_base64(mime)
msg.attach(mime)
Sending Embedded Images
To embed images in HTML emails, reference them using cid
:
python
msg.attach(MIMEText('<html><body><h1>Hello</h1><p><img src="cid:0"></p></body></html>', 'html', 'utf-8'))
Supporting Both HTML and Plain Text
Use MIMEMultipart('alternative')
to send both formats:
python
msg = MIMEMultipart('alternative')
msg.attach(MIMEText('hello', 'plain', 'utf-8'))
msg.attach(MIMEText('<html><body><h1>Hello</h1></body></html>', 'html', 'utf-8'))
Using Secure SMTP
For secure connections, such as with Gmail, use port 587 and start TLS:
python
smtp_server = 'smtp.gmail.com'
smtp_port = 587
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
Summary
Python's smtplib
makes sending emails straightforward. By mastering the construction of various email types and correctly setting headers, you can send complex emails with ease. Explore the email.mime documentation for detailed usage and capabilities.