Python Send mail example

Source: Internet
Author: User

Think of sending a message in Python

The main reason is that the server sometimes generates coredump files and then restarts after the server Coredump because of a script restart

But did not proactively notify the developer

I think I can write a script. Once the Coredump file is generated, you can send the message to the developer and let it know immediately

Here are just a few simple send scripts that you need to modify script Smtplib if you need to use them in a production environment. SMTP ([host[, port[, local_hostname[, timeout]])

The SMTP class constructor, which represents the connection to the SMTP server through which we can send instructions to the SMTP server to perform related operations such as logging in and sending mail. This class provides a number of methods, which are described below. All of its parameters are optional, where the host parameter represents the SMTP server host name, the SMTP host in the example above is "smtp.yeah.net";port represents the SMTP service ports, and the default is If both parameters are provided when creating the SMTP object, the Connect method is automatically called when initializing to connect to the server.
The Smtplib module also provides the Smtp_ssl class and the Lmtp class, and their operation is basically consistent with SMTP.
Smtplib. Methods that are provided by SMTP:

Smtp.set_debuglevel (Level)

Sets whether to debug mode. The default is false, which is non-debug mode, which means that no debug information is output.

Smtp.connect ([host[, Port]])

Connect to the specified SMTP server. The parameters represent Smpt hosts and ports, respectively. Note: You can also specify the port number (for example: SMPT.YEAH.NET:25) in the host parameter, so there is no need to give the port parameter.

Smtp.docmd (cmd[, argstring])

Sends instructions to the SMTP server. An optional parameter, argstring, represents the parameters of the directive. The following example simply sends instructions to the server by calling the DoCmd method to send the message (test pass on the Smtp.yeah.net mail server). Other mail servers have not tried):

  1. Import Smtplib, Base64, time
  2. UserName = base64.encodestring (' from '). Strip ()
  3. Password = base64.encodestring (' password '). Strip ()
  4. SMTP = Smtplib. SMTP ()
  5. Smtp.connect ("smtp.yeah.net:25")
  6. Print smtp.docmd (' helo ', ' from ')
  7. Print smtp.docmd (' auth login ')
  8. Print Smtp.docmd (userName)
  9. Print smtp.docmd (password)
  10. Print Smtp.docmd (' Mail from: ', ' <[email protected]> ')
  11. Print Smtp.docmd (' rcpt TO: ', ' <[email protected]> ')
  12. #data instruction indicates the contents of the message
  13. Print smtp.docmd (' data ')
  14. Print Smtp.docmd ("" From: [email protected]
  15. To: [Email protected]
  16. Subject:subject
  17. Email body
  18. .
  19. ‘‘‘)
  20. Smtp.quit ()
Smtp.helo ([hostname])

Use the "helo" directive to confirm the identity to the server. The equivalent of telling the SMTP server "who I am".

Smtp.has_extn (name)

Determines whether the specified name exists in the server mailing list. For security reasons, the SMTP server often blocks the directive.

Smtp.verify (Address)

Determines whether the specified e-mail address exists on the server. For security reasons, the SMTP server often blocks the directive.

Smtp.login (user, password)

Log on to the SMTP server. Almost all SMTP servers now have to allow messages to be sent after verifying that the user information is legitimate.

Smtp.sendmail (From_addr, To_addrs, msg[, Mail_options, Rcpt_options])

Send the message. Notice here that the third parameter, MSG, is a string that represents the message. We know that the mail is generally composed of the title, sender, recipient, mail content, attachments, etc., when sending mail, pay attention to the format of MSG. This format is the format defined in the SMTP protocol. In the above example, the value of MSG is:

    1. "From: [email protected]
    2. To: [Email protected]
    3. Subject:test
    4. Just for Test '

The meaning of this string means that the message sender is "[email protected]", the recipient is "[email protected]", the message is titled "Test" and the message is "just for test". Careful you may wonder: if the message to be sent is complex, including images, videos, attachments, etc., stitching strings in MIME format will be a hassle. Don't worry, Python has taken this into account, providing us with an email module that makes it easy to send messages with complex content like images, videos, attachments, and more. After the introduction of the Smtplib module, I will briefly introduce the basic use of the email module.

Smtp.quit ()

Disconnecting from the SMTP server is equivalent to sending a "quit" instruction.

e-mail and its related sub-modules

The Emial module is used to process mail messages, including MIME and other RFC 2822 -based message documents.

Using these modules to define the contents of the message is very simple. Here are some common classes:

Classemail.mime.multipart.MIMEMultipart: 多个MIME对象的集合。

Classemail.mime.audio.MIMEAudio: MIME音频对象。

Classemail.mime.image.MIMEImage: MIME二进制文件对象。

Classemail.mime.text.MIMEText: MIME文本对象。

#!/usr/bin/python#-*-coding:utf-8-*- ImportSmtplibImport Time fromEmail.mime.textImportMimetext fromEmail.mime.multipartImportMimemultipart#recipient listMail_namelist = ["[email protected]","[email protected]"] #Sending party informationMail_user ="e-mail address"Mail_pass="Email Password" #message HeaderMail_subject ="python sends test files"#Message text contentMail_context ="is the message content ~ ~ Ooxx"   defSend_main (): Msg=Mimemultipart () msg[' from'] =Mail_user msg[' to'] =";". Join (mail_namelist) msg['Subject'] =Mail_subject#Add message Contenttxt = mimetext ("This is the message content ~ ~ Ooxx") Msg.attach (TXT)#Send mailSMTP=Smtplib. SMTP () Smtp.connect ('smtp.qq.com:25') Smtp.login (Mail_user, Mail_pass) smtp.sendmail (Mail_user, Mail_namelist, Msg.as_string ()) SMT P.quit ()Print('message sent successfully') if __name__=='__main__': Send_main ()

Python Send mail example

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.