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