Python serial 56- sent with an attachment, the body of an HTML e-mail

A, HTML format how to send the right

1. Prepare the HTML code as content

2. Mail subtype set to html

3. Send

4. For example: send yourself a HTML file formats

 

from email.mime.text import MIMEText


main_content = """

        <!DOCTYPE html>

        <html lang = "en"

        <head>

            <meta charset = "UTF-8">

            <Title> Example </ title>

        </head>

        <body>

            <H1> This is a test used to do html <h1>

        </body>

        </html>

        """


msg = MIMEText(main_content,"html","utf-8")


# Construction of the sender's address and login information 

from_addr = " [email protected] "

from_pwd = ""

# Build information e-mail recipients 

to_addr = " [email protected] "

smtp_srv = "smtp.qq.com"

try:

    import smtplib

    srv = smtplib.SMTP_SSL(smtp_srv.encode(),465)

    srv.login(from_addr,from_pwd)

    srv.sendmail(from_addr,[to_addr],msg.as_string())

    srv.quit()


except Exception as a:

    print(a)

Second, send e-mail with attachments

1. Can the message be seen as a fit and a text message attachments

2. If a message involves multiple parts, you need to use to build MIMEMultipart format

3. Add a text MIMEText

4. Add a MIMEBase as an attachment or MEMEText

5. For example:

 

from email.mime.text Import MimeText # construct Accessories

from email.mime.multipart Import MIMEBase, MimeMultipart # constructs messages using the base


mail_mul = MimeMultipart () # constructing a mail object 

mail_text = MimeText ( " the Hello, the I AM liudana " , " Plain " , " UTF-. 8 " ) # Construction message body 

mail_mul.attach (mail_text) # to the constructed message body attached to the message

# Build attachments, accessories need to read from the local

# Open a local file

# Open to rb format 

with Open ( " 00.TestCasePython.py " , " rb " ) AS f:

    s = f.read()

    # Set the file name and MIME attachments 

    m = MimeText (S, " Base64 " , " UTF-8 " ) # type is base64, which is the format of the message body, here only need to remember it 

    m [ " Content-Type " ] = " file application / OCTET-Stream "

    # Note

    # After 1.attachment semicolon bit English state

    # 2.filename wrapped back quotes are needed, and attention shifted outside quotes 

    m [ " the Content-Disposition " ] = " Attachment; filename = '00 .TestCasePython.py ' "

    # Add to MIMEMultipart

    mail_mul.attach(m)


# Construction of the sender's address and login information 

from_addr = " [email protected] "

from_pwd = "ysqmojzwkgfciccd"

# Build information e-mail recipients 

to_addr = " [email protected] "

smtp_srv = "smtp.qq.com"

try:

    import smtplib

    srv = smtplib.SMTP_SSL(smtp_srv.encode(),465)

    srv.login(from_addr,from_pwd)

    srv.sendmail(from_addr,[to_addr],mail_mul.as_string())

    srv.quit()


except Exception as a:

    print(a)

Third, the source

D55_2_HTMLMailSend.py

D55_3_SendAttachmentMail.py

https://github.com/ruigege66/Python_learning/blob/master/D55_2_HTMLMailSend.py

https://github.com/ruigege66/Python_learning/blob/master/D55_3_SendAttachmentMail.py

2.CSDN:https://blog.csdn.net/weixin_44630050

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12008780.html