Hướng dẫn python email body from file - nội dung email python từ tệp
Ngày đăng:23/11/2022
Trả lời:0
Lượt xem:97
Tại sao bạn lại vượt qua một cơ thể không có thật nếu đó không phải là điều bạn muốn?
Mã của bạn dường như được viết cho Python 3.5 hoặc sớm hơn. Thư viện email đã được đại tu trong 3.6 và bây giờ khá linh hoạt và logic hơn một chút. Có thể vứt bỏ những gì bạn có và bắt đầu lại với các ví dụ từ tài liệu email.
Đây là một nỗ lực ngắn gọn.
from email.message import EmailMessage
...
message = EmailMessage()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# No point in using Bcc if the recipient is already in To:
with open(filename) as fp:
message.set_content(fp.read(), 'html')
# no need for a context if you are just using the default SSL
with smtplib.SMTP_SSL(smtp_server, 465) as server:
server.login(sender_email, password)
# Prefer the modern send_message method
server.send_message(message)
Nếu bạn muốn gửi một tin nhắn trong cả văn bản thuần túy và HTML, các ví dụ được liên kết chỉ cho bạn cách điều chỉnh mã để làm điều đó, nhưng sau đó, phần cơ thể
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
1 thực sự sẽ chứa một thông điệp hữu ích, không chỉ là một trình giữ chỗ.
Như đã nhận xét trong mã, không có lý do gì để sử dụng
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
2 nếu bạn đã chỉ định người nhận trong tiêu đề
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
3. Nếu bạn muốn sử dụng
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
2, bạn sẽ phải đặt một thứ khác vào tiêu đề
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
3, thường là địa chỉ của riêng bạn hoặc danh sách địa chỉ như
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
6
Một cách tiếp tuyến, khi mở tệp, Python (hoặc trên thực tế là hệ điều hành) kiểm tra thư mục làm việc hiện tại của người dùng, chứ không phải thư mục mà tập lệnh Python được tải. Có lẽ xem thêm chính xác những gì thư mục làm việc hiện tại là gì?
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
7: Ví dụ Jo
Dưới đây là một vài ví dụ về cách sử dụng gói
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
7 để đọc, viết và gửi tin nhắn email đơn giản, cũng như các tin nhắn MIME phức tạp hơn.
Trước tiên, hãy để Lừa xem cách tạo và gửi một tin nhắn văn bản đơn giản (cả nội dung văn bản và địa chỉ có thể chứa các ký tự Unicode):
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
Tiêu đề phân tích cú pháp RFC 822 có thể dễ dàng được thực hiện bằng cách sử dụng các lớp từ mô -đun
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
9:RFC 822 headers can easily be done by the using the classes from the
# Import smtplib for the actual sending functionimportsmtplib# Import the email modules we'll needfromemail.messageimportEmailMessage# Open the plain text file whose name is in textfile for reading.withopen(textfile)asfp:# Create a text/plain messagemsg=EmailMessage()msg.set_content(fp.read())# me == the sender's email address# you == the recipient's email addressmsg['Subject']=f'The contents of {textfile}'msg['From']=memsg['To']=you# Send the message via our own SMTP server.s=smtplib.SMTP('localhost')s.send_message(msg)s.quit()
9 module:
# Import the email modules we'll needfromemail.parserimportBytesParser,Parserfromemail.policyimportdefault# If the e-mail headers are in a file, uncomment these two lines:# with open(messagefile, 'rb') as fp:# headers = BytesParser(policy=default).parse(fp)# Or for parsing headers in a string (this is an uncommon operation), use:headers=Parser(policy=default).parsestr('From: Foo Bar <>\n''To: <>\n''Subject: Test message\n''\n''Body would go here\n')# Now the header items can be accessed as a dictionary:print('To: {}'.format(headers['to']))print('From: {}'.format(headers['from']))print('Subject: {}'.format(headers['subject']))# You can also access the parts of the addresses:print('Recipient username: {}'.format(headers['to'].addresses[0].username))print('Sender name: {}'.format(headers['from'].addresses[0].display_name))
Dưới đây, một ví dụ về cách gửi tin nhắn MIME chứa một loạt các hình ảnh gia đình có thể đang nằm trong một thư mục:
# Import smtplib for the actual sending function.importsmtplib# Here are the email package modules we'll need.fromemail.messageimportEmailMessage# Create the container email message.msg=EmailMessage()msg['Subject']='Our family reunion'# me == the sender's email address# family = the list of all recipients' email addressesmsg['From']=memsg['To']=', '.join(family)msg.preamble='You will not see this in a MIME-aware mail reader.\n'# Open the files in binary mode. You can also omit the subtype# if you want MIMEImage to guess it.forfileinpngfiles:withopen(file,'rb')asfp:img_data=fp.read()msg.add_attachment(img_data,maintype='image',subtype='png')# Send the email via our own SMTP server.withsmtplib.SMTP('localhost')ass:s.send_message(msg)
Dưới đây, một ví dụ về cách gửi toàn bộ nội dung của thư mục dưới dạng email: 1
#!/usr/bin/env python3"""Send the contents of a directory as a MIME message."""importosimportsmtplib# For guessing MIME type based on file name extensionimportmimetypesfromargparseimportArgumentParserfromemail.messageimportEmailMessagefromemail.policyimportSMTPdefmain():parser=ArgumentParser(description="""\Send the contents of a directory as a MIME message.Unless the -o option is given, the email is sent by forwarding to your localSMTP server, which then does the normal delivery process. Your local machinemust be running an SMTP server.""")parser.add_argument('-d','--directory',help="""Mail the contents of the specified directory, otherwise use the current directory. Only the regular files in the directory are sent, and we don't recurse to subdirectories.""")parser.add_argument('-o','--output',metavar='FILE',help="""Print the composed message to FILE instead of sending the message to the SMTP server.""")parser.add_argument('-s','--sender',required=True,help='The value of the From: header (required)')parser.add_argument('-r','--recipient',required=True,action='append',metavar='RECIPIENT',default=[],dest='recipients',help='A To: header value (at least one required)')args=parser.parse_args()directory=args.directoryifnotdirectory:directory='.'# Create the messagemsg=EmailMessage()msg['Subject']=f'Contents of directory {os.path.abspath(directory)}'msg['To']=', '.join(args.recipients)msg['From']=args.sendermsg.preamble='You will not see this in a MIME-aware mail reader.\n'forfilenameinos.listdir(directory):path=os.path.join(directory,filename)ifnotos.path.isfile(path):continue# Guess the content type based on the file's extension. Encoding# will be ignored, although we should check for simple things like# gzip'd or compressed files.ctype,encoding=mimetypes.guess_type(path)ifctypeisNoneorencodingisnotNone:# No guess could be made, or the file is encoded (compressed), so# use a generic bag-of-bits type.ctype='application/octet-stream'maintype,subtype=ctype.split('/',1)withopen(path,'rb')asfp:msg.add_attachment(fp.read(),maintype=maintype,subtype=subtype,filename=filename)# Now send or store the messageifargs.output:withopen(args.output,'wb')asfp:fp.write(msg.as_bytes(policy=SMTP))else:withsmtplib.SMTP('localhost')ass:s.send_message(msg)if__name__=='__main__':main()
Dưới đây, một ví dụ về cách giải nén thông báo MIME như tin nhắn ở trên, vào thư mục của các tệp:
#!/usr/bin/env python3"""Unpack a MIME message into a directory of files."""importosimportemailimportmimetypesfromemail.policyimportdefaultfromargparseimportArgumentParserdefmain():parser=ArgumentParser(description="""\Unpack a MIME message into a directory of files.""")parser.add_argument('-d','--directory',required=True,help="""Unpack the MIME message into the named directory, which will be created if it doesn't already exist.""")parser.add_argument('msgfile')args=parser.parse_args()withopen(args.msgfile,'rb')asfp:msg=email.message_from_binary_file(fp,policy=default)try:os.mkdir(args.directory)exceptFileExistsError:passcounter=1forpartinmsg.walk():# multipart/* are just containersifpart.get_content_maintype()=='multipart':continue# Applications should really sanitize the given filename so that an# email message can't be used to overwrite important filesfilename=part.get_filename()ifnotfilename:ext=mimetypes.guess_extension(part.get_content_type())ifnotext:# Use a generic bag-of-bits extensionext='.bin'filename=f'part-{counter:03d}{ext}'counter+=1withopen(os.path.join(args.directory,filename),'wb')asfp:fp.write(part.get_payload(decode=True))if__name__=='__main__':main()
Ở đây, một ví dụ về cách tạo thông báo HTML với phiên bản văn bản đơn giản thay thế. Để làm cho mọi thứ thú vị hơn một chút, chúng tôi bao gồm một hình ảnh liên quan trong phần HTML và chúng tôi lưu một bản sao của những gì chúng tôi sẽ gửi đến đĩa, cũng như gửi nó.
#!/usr/bin/env python3importsmtplibfromemail.messageimportEmailMessagefromemail.headerregistryimportAddressfromemail.utilsimportmake_msgid# Create the base text message.msg=EmailMessage()msg['Subject']="Ayons asperges pour le déjeuner"msg['From']=Address("Pepé Le Pew","pepe","example.com")msg['To']=(Address("Penelope Pussycat","penelope","example.com"),Address("Fabrette Pussycat","fabrette","example.com"))msg.set_content("""\Salut!Cela ressemble à un excellent recipie[1] déjeuner.[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718--Pepé""")# Add the html version. This converts the message into a multipart/alternative# container, with the original text message as the first part and the new html# message as the second part.asparagus_cid=make_msgid()msg.add_alternative("""\
Salut!
Cela ressemble à un excellent
recipie déjeuner.""".format(asparagus_cid=asparagus_cid[1:-1]),subtype='html')# note that we needed to peel the <> off the msgid for use in the html.# Now add the related image to the html part.withopen("roasted-asparagus.jpg",'rb')asimg:msg.get_payload()[1].add_related(img.read(),'image','jpeg',cid=asparagus_cid)# Make a local copy of what we are going to send.withopen('outgoing.msg','wb')asf:f.write(bytes(msg))# Send the message via local SMTP server.withsmtplib.SMTP('localhost')ass:s.send_message(msg)
Nếu chúng tôi được gửi tin nhắn từ ví dụ cuối cùng, đây là một cách chúng tôi có thể xử lý nó:
importosimportsysimporttempfileimportmimetypesimportwebbrowser# Import the email modules we'll needfromemailimportpolicyfromemail.parserimportBytesParserdefmagic_html_parser(html_text,partfiles):"""Return safety-sanitized html linked to partfiles. Rewrite the href="cid:...." attributes to point to the filenames in partfiles. Though not trivial, this should be possible using html.parser. """raiseNotImplementedError("Add the magic needed")# In a real program you'd get the filename from the arguments.withopen('outgoing.msg','rb')asfp:msg=BytesParser(policy=policy.default).parse(fp)# Now the header items can be accessed as a dictionary, and any non-ASCII will# be converted to unicode:print('To:',msg['to'])print('From:',msg['from'])print('Subject:',msg['subject'])# If we want to print a preview of the message content, we can extract whatever# the least formatted payload is and print the first three lines. Of course,# if the message has no plain text part printing the first three lines of html# is probably useless, but this is just a conceptual example.simplest=msg.get_body(preferencelist=('plain','html'))print()print(''.join(simplest.get_content().splitlines(keepends=True)[:3]))ans=input("View full message?")ifans.lower()[0]=='n':sys.exit()# We can extract the richest alternative in order to display it:richest=msg.get_body()partfiles={}ifrichest['content-type'].maintype=='text':ifrichest['content-type'].subtype=='plain':forlineinrichest.get_content().splitlines():print(line)sys.exit()elifrichest['content-type'].subtype=='html':body=richestelse:print("Don't know how to display {}".format(richest.get_content_type()))sys.exit()elifrichest['content-type'].content_type=='multipart/related':body=richest.get_body(preferencelist=('html'))forpartinrichest.iter_attachments():fn=part.get_filename()iffn:extension=os.path.splitext(part.get_filename())[1]else:extension=mimetypes.guess_extension(part.get_content_type())withtempfile.NamedTemporaryFile(suffix=extension,delete=False)asf:f.write(part.get_content())# again strip the <> to go from email form of cid to html form.partfiles[part['content-id'][1:-1]]=f.nameelse:print("Don't know how to display {}".format(richest.get_content_type()))sys.exit()withtempfile.NamedTemporaryFile(mode='w',delete=False)asf:f.write(magic_html_parser(body.get_content(),partfiles))webbrowser.open(f.name)os.remove(f.name)forfninpartfiles.values():os.remove(fn)# Of course, there are lots of email messages that could break this simple# minded program, but it will handle the most common ones.
Cho đến lời nhắc, đầu ra từ trên là:
To: Penelope Pussycat <>, Fabrette Pussycat <>
From: Pepé Le Pew <>
Subject: Ayons asperges pour le déjeuner
Salut!
Cela ressemble à un excellent recipie[1] déjeuner.
Chú thích
1
Cảm ơn Matthew Dixon Cowles vì cảm hứng và ví dụ ban đầu.