General e-Mail method
I used to do this when I was using Python to automate the Mail function:
Import SmtplibFrom Email.mime.textImport MimetextFrom Email.headerImport Header# Send Mailbox Server SmtpServer=' Smtp.sina.com '# Send mailbox user/password user=' [email protected] ' password=' 123456 '# Send Mailbox Sender=' [Email protected] '# Receive Mailbox Receiver=' [Email protected] '# send mail subject subject = " Python Email test ' # writing HTML type message body MSG = Mimetext ( ' html ', ' utf-8 ') Msg[ ' Subject '] = Header (subject, ' utf-8 ') # Connect Send mail SMTP = smtplib. SMTP () SMTP. connect (smtpserver) smtp.login (user, password) smtp.sendmail (sender, receiver, msg.as_string ()) Smtp.quit ()
In fact, this code is not complicated, as long as you understand the use of email messages, then the following questions are what you have to consider:
- Your login e-mail account/password
- Each other's email account
- Message content (title, body, attachments)
- Mailbox Server (smtp.xxx.com/pop3.xxx.com)
Yagmail Implementing Outgoing Mail
Yagmail can be more simple to implement the automatic e-mail function.
GitHub Project Address: Https://github.com/kootenpv/yagmail
Installation
install yagmail
Simple example
Import Yagmail#链接邮箱服务器yag= yagmail. SMTP (User= "[email protected]", Password= "1234", Host= ' smtp.126.com ') # Mailbox Body contents = [ ' This was the body, and here is just text http://somedomain/image.png ', ' can find an audio file attached. ', '/local/path/song.mp3 '] # Send mail Yag.send ( "[Email protected] ', ' subject ', contents)
A total of four lines of code, is not much simpler than the above example.
Send mail to multiple users
# 发送邮件yag.send([‘[email protected]‘,‘[email protected]‘,‘[email protected]‘], ‘subject‘, contents)
You only need to turn the receiving mailbox into a list.
Send a message with an attachment
# 发送邮件yag.send(‘[email protected]‘, ‘发送附件‘, contents, ["d://log.txt","d://baidu_img.jpg"])
Just add a list of attachments to send.
I was almost moved to cry, where to find such a conscience library to go? Simple is a bit unlike programming language!
Python Automatic Email library yagmail