Python Automatic Email library yagmail

Source: Internet
Author: User
Tags email account mail account

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

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.