Python Email (1)

Source: Internet
Author: User
Tags email account

python send mail

1. Send email steps via python:

Prerequisite: A third-party license is enabled and SMTP service can be used

1. Creating an SMTP Object

2. Connect the SMTP server, the default port number is 25

3. Login to your email account

4. Call the Send Message function, Parameters: sender, recipient, message content

5. Close the connection


2. Mail Message registration:

First create a Message object:

msg = Email.mime.multipart.MIMEMultipart () #通过这个类创建消息

Msg[' from '] = ' [email protected] '

Msg[' to '] = ' [email protected]; [Email protected]; [Email protected] '

msg[' subject '] = ' aji111 '

Indicate the sender of the message, and the receipt, which represents only the problem displayed, such as:


3. Message content:

First, define a string that represents your message content:

context= ' Hello World ' # # # text

txt = email.mime.text.MIMEText (_text=content, _subtype= "html") # # After defining the text, indicate what format to designate the context to, html,txt

Msg.attach (TXT) # # re-registered in the message

_subtype This parameter is decided, you are in the form of HTML parsing to send, or in the form of text to send.



Prepare the file:

(1) Test configuration file:

File 1:message.conf (config file)

from = [email protected]to = [email protected],[email protected],[email protected],[email Protected],[email protected] Subject = ' Test message ' message = ' Hello everyone: Test mail test message above thank you '


(2) Read the file Conf file tool and test

File 2:util.py (tool file)

import codecsimport refilename =  "message.conf" Def getproperty (property):     with codecs.open (filename, encoding= "Utf-8")  as f:         if property !=  "Message":             for line in f.readlines ():                 if line.startswith ("{0} = ". Format ( property)):                     value = line.split ("{0} = ". Format (property)) [1]                     print  value.strip (' \ n ')                      return value.strip (' \ n ')         else:             reg = re.compile (R "message = " (. * \ \) "")             result =  Reg.findall (F.read ())             print result [0] [0]            return  result[0][0] GetProperty ("from") GetProperty ("to") GetProperty ("Subject") getProperty ("message")

File 2 Call the parameters of the file 1, get to the corresponding value, test results

When the test is complete, unregister the output information and call the function


(3) Send mail script

3. File:sendtext.py (Mail script file)

Import email.mime.multipartimport email.mime.textimport email.headerfrom util import Getpropertyimport smtplib# Message content msg = Email.mime.multipart.MIMEMultipart () Sendfrom = GetProperty ("from") SendTo = GetProperty ("to") Sendsubject = GetProperty ("Subject") SendMessage = GetProperty ("message") msg["from"] = sendfrommsg["to"] = sendtomsg[' Subject '] = Email.header.Header (sendsubject) Text = Email.mime.text.MIMEText (sendMessage, ' plain ', ' utf-8 ') Msg.attach (text) # Send SMTP = Smtplib. Smtp_ssl ("smtp.qq.com", 465) smtp.login (' [email protected] ', ' xrusbcaae ') smtp.sendmail (Sendfrom, Sendto.split (","), Msg.as_string ()) Smtp.quit ()

To perform a Send message:

Python Email (1)

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.