Python automatic e-mail-yagmail library

Source: Internet
Author: User

Previously wrote using the standard library to use Python smtplib and email to send mail, feeling very cumbersome, long time no later forget. What Python libraries Do you brief encounter the other day? See the yagmail third-party library, the learning process encountered some problems, recorded here.

The first step of the Python smtplib, e-mail module module that was used earlier is:

First, import smtplib module  import mimetext Library to make plain text mail template
Two, send the message several related parameters, each mailbox of the outgoing server is not the same, to 126 as an example of Baidu Search server is smtp.126.com
Third, write the subject and body of the message, where the text is in HTML format
Iv. final call to SMTP send service

Look at the code:
Import UUIDImportSmtplib fromEmail.mime.textImportMimetext#send e-mail related parametersSmtpsever ='smtp.126.com'Sender=' Sender@126.com'PSW="[Email protected]"            #126 Email Authorization CodeReceiver =' receiver@qq. com'#Edit the contents of a message#subject=u "NBA"body=Str (UUID.UUID4 ()) MSG=mimetext (Body,'HTML','Utf-8') msg[' from']='[email protected]'msg[' to']=' Userename@qq. com'#msg[' subject ']=subjectTry: SMTP=Smtplib. SMTP () smtp.connect (smtpsever) smtp.login (SENDER,PSW) Smtp.sendmail (sender,receiver,msg.as_string ())Print("message sent successfully")exceptSmtplib. Smtpexception:Print("Error: Unable to send message")

To complete the above message delivery, you need the following information:

1. Outgoing server
2. Sender Account
3. Sender's email password [means authorization code]
4. Recipient Account
5. Edit the contents of the message [text, where, where]
6. Linked Mailbox servers
7. Login email [provide sender's account and password (refer to authorization code)]
8. Send mail

It seems that the whole process is a little bit complicated, but it's OK, after all, the function can be realized ...

But the appearance of Yagmail will subvert some people's impression of email, because Yagmail compared to email, the implementation is really too simple, yagmail on GitHub home page gives a brief introduction of Yagmail and use. A concise example is given below:

Yagmail Installation

Pip Install Yagmail

Send a mailbox to a single recipient

ImportYagmail#Linked Mailbox ServersYAG = Yagmail. SMTP (user="[email protected]", password="126 Email Authorization Code", host='smtp.126.com')#Mailbox Bodycontents = ['the body, and here are just text http://somedomain/image.png',            'You can find a audio file attached.','/local/path/song.mp3']#Send mailYag.send ([email protected]', 'Subject', Contents)

Send the results as follows:

Compare email, SMTP module, the implementation of Yagmail is really too simple, moved to cry ~ ~ ~

send mail to multiple recipients

# Send mail yag.send (['[email protected]','[email protected]' ,'[email protected]'subject', contents)

In () add more than one recipient's mailbox, other parameters do not need to modify, is not very simple ~ ~ ~

Send a message with an attachment

Yag.send ('[email protected]' send attachments ', contents, ["e:// Whitelist.txt","e://baidu_img.jpg"])

The above ["E://whitelist.txt", "e://baidu_img.jpg"] is the path to upload the attachment. The returned results are as follows:

Python automatic e-mail-yagmail library

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.