Use Python to secretly send mail

Source: Internet
Author: User

The previous company to work on the Internet or send and receive mail have to be cautious, afraid of inadvertently be observant to see, and have to be said. In order to be able to send an email without being found, hey, I wrote a mail with Python program, with the control console, do not understand people must think that brother is still programming work. Ha ha.

Here's a brief description of how to use Python to send messages, including plain text content, or an attachment, or a message with HTML content. It can be said that with Python, everything becomes very easy.

The Smtplib module is a standard module for sending emails, and it is very simple to implement with several modules within the email.mime.

def sendmail (message):
    try:
        smtp = Smtplib. SMTP (email_host)
        smtp.login (Email_user, email_pwd)  
        smtp.sendmail (email_user+ "@" +email_postfix, TO_EMAIL, Message)  
        smtp.quit ()  
        print ' email send success. '
    Except Exception, E:
        print e
        print ' email send failed. '
First to import the Smtplib module, add import smtplib, initialize the time directly connected to the mailbox server, that is, the above email_host (I use 163 mailbox, so the definition of email_host = ' smtp.163.com '), Because the current mailbox basically needs to log on to use, so to invoke the Smtp.login () function, enter the user and password.

Def sendwithoutattachment ():
    msg = Mimetext (GetContent (), ' Plain ', ' utf-8 ')
    getheader (msg)
    
    SendMail ( Msg.as_string ())

I will send the simple text content of the message alone as a function, using mimetext generation, note that here is used utf-8, because the content may be Chinese, so to specifically specify. If you want to send HTML content, then speak plain change to HTML.

def getheader (msg):
    msg[' from '] = ME
    msg[' to '] = ";". Join (to_email)
    msg[' Subject '] = email_header
    msg[' Date ' = FormatDate (localtime=true)

The GetHeader function is used to set the sender, recipient, subject, and sending time.

Def getcontent ():
    path = OS.GETCWD ()
    file = Os.path.join (path, content_file_name)
    CONTENT = open (file, ' RB '
    data = Content.read ()
    try:
        data = Data.decode (' GBK ')
    except:
        data = Data.decode (' GBK ', ' ignore ') )
    Content.close () return
    data
As for the message body, I was written in advance to a TXT document, read out. This is also more covert. : The idea is to encode Chinese.

def getattachment (msg):
    CType, encoding = Mimetypes.guess_type (Accessory_fullpath)
    If CType is None or encoding is not None:  
        ctype = ' Application/octet-stream '  
    maintype, subtype = ctype.split ('/', 1)

    #Formating Accessory data
    data = open (Accessory_fullpath, ' RB ')
    file_msg = Mimebase (MainType, subtype)
    file_msg.set_ Payload (Data.read ())
    data.close ()
    encode_base64 (file_msg) 
    #file_msg ["content-type"] = CType # if add Type then return error 10054
    file_msg.add_header (' content-disposition ', ' attachment ', filename = accessory_name) C12/>msg.attach (FILE_MSG)
Attachments are also created independently of a function, but note that you do not specify a "Content-type" type, or you cannot send a message. The problem has not been solved yet.


The above basically includes sending the mail the main several functions, the concrete smtplib module and the mime and so on content, the material is many, here does not explain in detail, has the interest to be able to search the Internet.

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.