python Realizes Mailbox Sending Information

  • 2021-11-24 02:03:02
  • OfStack

This article example for everyone to share python to achieve the specific code of mailbox to send information, for your reference, the specific content is as follows

1. SSL

SSL refers to Secure Sockets Layer. In short, it is a standard technology that can ensure the security of Internet connection, protect any sensitive data sent between two systems, and prevent cybercriminals from reading and modifying any transmitted information, including personal data. Two systems may mean a server and a client (for example, a browser and a shopping website), or between two servers (for example, an application with personally identifiable information or payroll information).

2. SMTP

SMTP (Simple Mail Transfer Protocol) is the Simple Mail Transfer Protocol, which is a set of rules for sending messages from source address to destination address, and it controls the transit mode of messages.

python's smtplib provides a convenient way to send e-mail. It simply encapsulates smtp protocol.

3. Achieve mailbox bombing

Three modules are needed

1. SMTP_SSL-Encrypted and secure transmission. smtplib can also be used, but SSL is recommended

2. MIMEText-Used to return objects (which are easy to operate) and store the written contents

3. Header-Used for anti-detection. If you don't write it, the mailbox may be prompted as junk mailbox, so you can't send information

Step 3:

1. Login (SMTP_SSL)-To instantiate the SMTP_SSL object

2. Send the written information (sendmail (starthost, endhost, me.as_string ())-starthost is

3. Exit (quit)


'''
 Parameter description: 
    host: SMTP  Server host.   You can specify the host's ip Address or domain name such as :runoob.com This is an optional parameter. 
    port:  If you provide  host  Parameter ,  You need to specify  SMTP  The port number used by the service, 1 Under normal circumstances SMTP Port number is 25 . 
    local_hostname:  If SMTP On your local machine, you only need to specify the server address as  localhost  That's enough. 
'''
import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )

'''
 Parameter description: 
    from_addr:  Message sender address. 
    to_addrs:  String list, mail sending address. 
    msg:  Send a message 
Python SMTP Object uses sendmail Method, the syntax is as follows: 
'''
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]

Implementation method:

Use the third party mailbox service:

Columns such as using your own qq mailbox service to send (anonymous) mailbox information to the other party (or yourself)

But you have to get the authorization code of QQ mailbox first (there is a tutorial on the Internet, which is very simple)


'''
    Python  Mailbox   Late night test 
     Authorization code: *****
'''
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header

# No. 1 3 Party services ( qq ) 
mail_host = 'smtp.qq.com'
mail_user = '27*****' # Account number 
mail_pws = '*****' # Authorization code 

# Login 
smtp = SMTP_SSL(mail_host)  # Instantiation 
smtp.ehlo(mail_host)
smtp.login(mail_user,mail_pws)# Login 

# My email address and the other party's email address 
my_mail = '27*****@qq.com' # I typed my own email address 
her_mail = '*****@163.com' # E-mail address of the other party 

# What to write 
cont = ' Hello, I am HV I'm from an alien planet! '
# Title 
title = ' It's from the earth HV'

# Content formatting 
for i in range(10): # Send to the target 10 Mailbox 
    msg = MIMEText(cont,'plain','UTF-8') #plain For txt Format, if you write directly txt It will be detected as junk mailbox 
    msg['Subject'] = Header(title,'UTF-8') 
    msg['From'] = my_mail # Fill in your email address format here, and the other party will know who the sender is, otherwise the other party defaults to (no sender) 
    msg['To'] = ' My good friend ' # Pay attention to this! You can only fill in strings here. If you fill in other data types, you will report errors! 
    smtp.sendmail(my_mail,her_mail,msg.as_string())
    # smtp.quit()

smtp.quit() # Shut down SMTP Mailbox information transfer 

Of course, it can also be encapsulated in a function for easy calling


Related articles: