β

python利用sender群发邮件

Burness Duan 161 阅读

首先必须感谢github上的fengsp写的sender,确实不错,所以用它写的这个包来学习下python写怎么发送邮件!!!

sender地址见https://github.com/fengsp

sender很简单,主要的API包括:

class sender.Mail(host=’localhost’, username=None, password=None, port=25, use_tls=False, use_ssl=False, debug_level=None, fromaddr=None)

Sender Mail main class. This class is used for manage SMTP server connections and send messages.

Parameters:

host – smtp server host, default to be ‘localhost’

username – smtp server authentication username

password – smtp server authentication password

port – smtp server port, default to be 25

use_tls – put the SMTP connection in TLS (Transport Layer Security) mode, default to be False

use_ssl – put the SMTP connection in SSL mode, default to be False

debug_level – the debug output level

fromaddr – default sender for all messages sent by this mail instance

connection

Open one connection to the SMTP server.

send(message_or_messages)

Sends a single messsage or multiple messages.

Parameters: message_or_messages – one message instance or one iterable of message instances.

send_message(*args, **kwargs)

Shortcut for send.

class sender.Message(subject=None, to=None, body=None, html=None, fromaddr=None, cc=None, bcc=None, attachments=None, reply_to=None, date=None, charset=’utf-8′, extra_headers=None, mail_options=None, rcpt_options=None)

One email message.

Parameters:

subject – message subject

to – message recipient, should be one or a list of addresses

body – plain text content body

html – HTML content body

fromaddr – message sender, can be one address or a two-element tuple

cc – CC list, should be one or a list of addresses

bcc – BCC list, should be one or a list of addresses

attachments – a list of attachment instances

reply_to – reply-to address

date – message send date, seconds since the Epoch, default to be time.time()

charset – message charset, default to be ‘utf-8’

extra_headers – a dictionary of extra headers

mail_options – a list of ESMTP options used in MAIL FROM commands

rcpt_options – a list of ESMTP options used in RCPT commands

attach(attachment_or_attachments)

Adds one or a list of attachments to the message.

Parameters: attachment_or_attachments – one or an iterable of attachments

attach_attachment(*args, **kwargs)

Shortcut for attach.

class sender.Attachment(filename=None, content_type=None, data=None, disposition=’attachment’, headers={})

File attachment information.

Parameters:

filename – filename

content_type – file mimetype

data – raw data

disposition – content-disposition, default to be ‘attachment’

headers – a dictionary of headers, default to be {}

主要就是三大块,首先是Mail()主要是管理SMTP服务器连接。成员函数包括connection、send、send_message,其中,send和send_message在于send的参数是Message类对象,而send_message是直接参数内容:

from sender import Mail

mail = Mail()

mail.send_message(“Hello”, fromaddr=”from@example.com”,

to=”to@example.com”, body=”Hello world!”)

mail.send(msg)

or an iterable of messages

mail.send([msg1, msg2, msg3])

第二个类是Message主要用于构造一个邮件内容:包括主题、邮件地址、cc地址、内容、附件、日期、charset等等。其成员函数attach用于添加一个或者多个邮件附件的信息、attach_attachment和send与send_message()的差别差不多!!!

第三个类Attachment用于构造附件的信息:

attachment = Attachment(“logo.jpg”, “image/jpeg”, f.read())

在基于sender写了一个群发的程序,还蛮有意思的!!!并且发送附件!!!!

from sender import Mail

import time

in email_server_config.txt, you enter your email information in this format: smtp.example.com,25,username,password

with open(‘./email_server_config.txt’,’r’) as f:

config_content=[]

for line in f.readlines():

config_content=line.split(“,”)

smtp_server=config_content[0]

smtp_server_port=config_content[1]

smtp_server_username=config_content[2]

smtp_server_password=config_content[3]

mail=Mail(smtp_server,port=smtp_server_port,username=smtp_server_username,password=smtp_server_password,

use_tls=False,use_ssl=False,debug_level=None)

from sender import Message

with open(‘./to_email_list.txt’,’r’) as f:

for line in f.readlines():

msg=Message(“email subject”,fromaddr=(yourname,smtp_server_username),to=line)

msg.body = “this is a msg plain text body”

msg.date = time.time()

msg.charset = “utf-8”

msg.extra_headers = {}

msg.mail_options = []

msg.rcpt_options = []

from sender import Attachment

with open(“to_email_list.txt”) as f:

attachment=Attachment(“to_email_list.txt”,”text/txt”,f.read())

msg.attach(attachment)

mail.send(msg)

print ‘To check in your email that ensure it is ok’

作者:Burness Duan
一个程序猿的小天地
原文地址:python利用sender群发邮件, 感谢原作者分享。

发表评论