Python sends a message (the message received must be sent to the sender to reply)

Source: Internet
Author: User
Tags rfc

Python uses SMTP (Simple Mail Transfer Protocol) to send plain text messages for ordinary text message delivery, the key is to set Mimetext _subtype to plain##-*-coding:utf-8-*-import Smtplibfrom email.mime.text Import mimetext# importing Mimetext class from email import encodersfrom email.header import Headerfrom Email.utils import parseaddr, formataddrmail_host= "smtp.163.com" #使用的邮箱的smtp服务器地址, here is the SMTP address of 163 mail_user=raw_input ("Your 163 mailbox:") #发送方邮箱mail_pass =raw_input ("Email Authorization Code:") #授权码mail_postfix = "163.com" #邮箱的后缀, NetEase is 163.commail_from = Raw_input ("From:") #发送方昵称mail_sub = Raw_input ("Message Subject:") #邮件主题mail_content = Raw_input ("content:") def send_mail (sub,content): Me=mai  L_from+ "<" +mail_user+ ">" msg = mimetext (content, ' plain ', ' utf-8 ') #邮件正文, subtype (text is plain), encoded format msg[' Subject '] = Sub Msg[' from ' = Me to_list = Map (None,raw_input ("mail_to:"). Split ("")) #可以群发 msg[' to '] = ",". Join (To_list) #将收 The list of items is separated by ', ' Try:server = Smtplib. SMTP () server.connect (mail_host) #连接服务器 server.login (mail_user,mail_pass) #登录操作 server.sendmail (Me, to_ List, msg.as_string ()) Server.close () return True except Exception, E: #捕捉异常, and output the exception object print str (e) return Falsei F Send_mail (Mail_sub, mail_content): #邮件主题和邮件内容 print "done!" Else:print "failed!" MIME (Multipurpose Internet Mail Extensions) multi-purpose Internet Mail extension type. Is the type of file that sets the extension of an application to be opened by an app, and when the extension file is accessed, the browser automatically opens with the specified application. Many are used to specify some client-customized file names, as well as some ways to open media files. It is an Internet standard that expands the e-mail standard to enable it to support: non-ASCII word literals, non-text format attachments (binary, sound, image, etc.), a message body consisting of multipart (multiple parts), header information containing non-ASCII characters (header Information). This standard is defined in RFC 2045, RFC 2046, RFC 2047, RFC 2048, RFC 2049, and so on. MIME improved RFC 2822, which was changed by RFC 822, which specifies that e-mail standards do not allow the use of characters other than the 7-bit ASCII character set in mail messages. Because of this, some non-English character messages and binary files, images, sounds and other non-text messages can not be transmitted in the E-mail (MIME). MIME defines a symbolic method for representing a wide variety of data types. In addition, a MIME framework is used in the HTTP protocol used in the World Wide Web, and the standard is extended to the Internet media type.
Import smtplibfrom email.mime.text import mimetext# used to format the mail address from Email.header import headerfrom email.utils Import PARSEADDR, formataddrmsg = Mimetext (' Someone else's tolerance is not for you to trample on '! ', ' plain ', ' utf-8 ') # Enter your email address and password: from_addr = Raw_input (' From ( 163 e-mail): ' #这里的密码一定是授权码, 163 mailbox original password not. Password = raw_input (' Authorization Code: ') # Enter the SMTP server address: Here we use Smtp.163.com#smtp_server = Raw_input (' SMTP server: ') smtp_server = "SMTP .163.com "# Enter recipient address: TO_ADDR = Raw_input (' to: ') def _format_addr (s): name, addr = parseaddr (s) #这个函数会解析出姓名和邮箱地址return Formataddr (Header (name, ' Utf-8 '). Encode (), Addr.encode (' Utf-8 ') if isinstance (addr, Unicode) Else addr) def Send_mail (): #设置发件人, Recipient name and message subject msg[' from '] = _format_addr (U ' joker <%s> '% from_addr) msg[' to '] = _format_addr (U ' friend <%s > '% to_addr) msg[' Subject ' = Header (U ' Test mail ', ' utf-8 '). Encode () Server = Smtplib. SMTP (SMTP_SERVER) # SMTP protocol The default port is 25#server.set_debuglevel (1) #打印出和SMTP服务器交互的所有信息server. Login (from_addr, password) #登录服务器 # Send mail, here the second parameter is a list, can have multiple recipients # The message body is a str,as_string () to turn the Mimetext object into StRserver.sendmail (FROM_ADDR, [to_addr], msg.as_string ()) Server.quit () return Trueif send_mail ():p rint "done!" 

Python sends a message (the message received must be sent to the sender to reply)

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.