Python Learning notes (SMTP mail sending)

Source: Internet
Author: User

Want to add mail to the framework, so tidy up under the Python mail send function

First Python is a mail-enabled send, built-in SMTP library, support for sending plain text, HTML, and adding attachments to messages

After the mailbox, such as 163, QQ, Sina and other mailboxes by default to shut down the SMTP service, we need to manually open

Sent through the sender's SMTP service after opening via sender's mailbox, authorized password

The code is as follows:

1 #!/usr/bin/env python2 #-*-coding:utf_8-*-3 4  fromEmail.mime.textImportMimetext5  fromEmail.mime.multipartImportMimemultipart6  fromEmail.mime.multipartImportMimebase7  fromEmailImportencoders8  fromEmail.headerImportHeader9  fromEmail.utilsImportparseaddr, FormataddrTen ImportSmtplib One  A  - classSendEmail: -Outbox ="[email protected]" the     #Outbox Address -Password ="wxqcl258258" -     #authorization password is not a mailbox login password -Inbox ="[email protected]" +     #Inbox Address -Smtp_server ="smtp.163.com" +     #Outbox Server Address A  at     def __init__(self): -         Pass -  - @classmethod -     def_format_address (CLS, text): -Name, address =parseaddr (text) in         returnFormataddr (Header (name,"Utf-8"). Encode (), address) -  to @classmethod +     defSend_email_text (CLS): -msg = Mimetext ("To test the SMTP mail delivery feature","Plain","Utf-8") the         #first parameter: message body *         #second parameter: Message type plain text $         #third parameter: encodingPanax Notoginseng  -msg[" from"] = sendemail._format_address ("an e-mail from 163 <%s>"%Sendemail.outbox) the         #sender name vs. Outbox Address +msg[" to"] = sendemail._format_address ("Administrators <%s>"%Sendemail.inbox) A         #recipient's name and Inbox address themsg["Subject"] = Header ("Greetings from SMTP","Utf-8"). Encode () +         #message Header -  $         Try: $Server = Smtplib. SMTP (Sendemail.smtp_server, 25) -             #constructing an SMTP server connection -             #server.set_debuglevel (1) the             #Debug output mode off by default - Server.login (Sendemail.outbox, Sendemail.password)Wuyi             #log on to the SMTP server the Server.sendmail (Sendemail.outbox, [Sendemail.inbox], msg.as_string ()) -             #Send mail Wu server.quit () -             Print "message sent successfully" About         exceptException, E: $             PrintStr (e) -             Print "message failed to send" -              - if __name__=='__main__': ASendemail.send_email_text ()

Outbox Effect:

Inbox effects

This is just plain text content, can support HTML format content, modify the content as follows:

msg = Mimetext ("Test SMTP mail Send function", "plain", "utf-8")

Content changed to HTML format, "plain" to "HTML"

Finally, the message that added the attachment

The code is as follows:

1 @classmethod2     defSend_email_multipart (CLS):3msg =Mimemultipart ()4 5msg[" from"] = sendemail._format_address ("an e-mail from 163 <%s>"%Sendemail.outbox)6         #sender name vs. Outbox Address7msg[" to"] = sendemail._format_address ("Administrators <%s>"%Sendemail.inbox)8         #recipient's name and Inbox address9msg["Subject"] = Header ("Greetings from SMTP","Utf-8"). Encode ()Ten         #message Header One  AMsg.attach (Mimetext ("test the SMTP mail delivery feature for adding attachments","Plain","Utf-8")) -  -With open ("E:\\work\\python Project\\createproject\\20160421140953.xml","RB") as F: the             #set MIME and file names for attachments -MIME = Mimebase ("XML","XML", filename="test Report. XML") -             #Add the necessary header information -Mime.add_header ('content-disposition','Attachment', filename="test Report. XML") +Mime.add_header ('Content-id','<0>') -Mime.add_header ('X-attachment-id','0') +             #read the contents of the attachment in: A Mime.set_payload (F.read ()) at             #code with BASE64: - Encoders.encode_base64 (MIME) -             #Add to Mimemultipart: - Msg.attach (MIME) -  -         Try: inServer = Smtplib. SMTP (Sendemail.smtp_server, 25) -             #constructing an SMTP server connection to             #server.set_debuglevel (1) +             #Debug output mode off by default - Server.login (Sendemail.outbox, Sendemail.password) the             #log on to the SMTP server * Server.sendmail (Sendemail.outbox, [Sendemail.inbox], msg.as_string ()) $             #Send mailPanax Notoginseng server.quit () -             Print "message sent successfully" the         exceptException, E: +             PrintStr (e) A             Print "message failed to send"

Inbox Effect:

Bloggers are also referring to the website of the Great God taught, is a full-fledged Python learning site

This gives the link address:

Liaoche Python Learning Address

Python Learning notes (SMTP mail sending)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.