A simple SMTP email realized by Python

refer to SMTP发送邮件
The simplest one:

from email.mime.text import MIMEText

msg = MIMEText('hello world','plain','utf-8')
sender_mail = input('sender mailbox:')
sender_passwd = input('sender mailbox passward:')
receiver_mail = input('receiver mailbox:')
smtp_addr = input('SMTP server address:')

import smtplib
smtpServer = smtplib.SMTP(smtp_addr,25)
smtpServer.set_debuglevel(1)
smtpServer.login(sender_mail,sender_passwd)
smtpServer.sendmail(sender_mail,[receiver_mail],msg.as_string())
smtpServer.quit()

I want to add the object of ‘From’,’To’ and ‘Subject’:

from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr,formataddr
import smtplib

def formatEmailAddr(rawData):
    name,addr = parseaddr(rawData)
    return formataddr((Header(name,'utf-8').encode(),addr))

sender_mail = input('sender mailbox:')
sender_passwd = input('sender mailbox passward:')
receiver_mail = input('receiver mailbox:')
smtp_addr = input('SMTP server address:')
#handle the content of this email
msg = MIMEText('hello world','plain','utf-8')
msg['From'] = formatEmailAddr('SMTP email sender<%s>' %(sender_mail))
msg['To'] = formatEmailAddr('SMTP email receiver<%s>' %(receiver_mail))
msg['Subject'] = Header('It is a learning code','utf-8').encode()

#send progress
smtpServer = smtplib.SMTP(smtp_addr,25)
smtpServer.set_debuglevel(1)
smtpServer.login(sender_mail,sender_passwd)
smtpServer.sendmail(sender_mail,[receiver_mail],msg.as_string())
smtpServer.quit()

I want to send the HTML pattern email:

#modify the content of the ‘MIMEText’ with 'html' pattern
msg = MIMEText('<html><body><h1>Hello</h1>' +
    '<p>send by <a href="http://www.python.org">Python</a>...</p>' +
    '</body></html>', 'html', 'utf-8')

I want to add an attachment,modify the msg which will be sent:

msg = MIMEMultipart()
msg['From'] = formatEmailAddr('SMTP email sender<%s>' %(sender_mail))
msg['To'] = formatEmailAddr('SMTP email receiver<%s>' %(receiver_mail))
msg['Subject'] = Header('It is a learning code','utf-8').encode()

msg.attach(MIMEText('hello world', 'plain', 'utf-8'))

with open('/Users/wuxiao/Downloads/test.png', 'rb') as f:
    mime = MIMEBase('image', 'png', filename='test.png')
    mime.add_header('Content-Disposition', 'attachment', filename='test.png')
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    mime.set_payload(f.read())
    encoders.encode_base64(mime)
    msg.attach(mime)

send both HTML and Plain pattern message:

msg = MIMEMultipart('alternative')
msg['From'] = formatEmailAddr('SMTP email sender<%s>' %(sender_mail))
msg['To'] = formatEmailAddr('SMTP email receiver<%s>' %(receiver_mail))
msg['Subject'] = Header('It is a learning code','utf-8').encode()

msg.attach(MIMEText('hello world', 'plain', 'utf-8'))
msg.attach(MIMEText('<html><body><h1>Hello World</h1></body></html>', 'html', 'utf-8'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值