Home > other >  For help in python hair with attachments qq mail, hair don't go out
For help in python hair with attachments qq mail, hair don't go out

Time:10-05

For help in python with attachments qq hair mail, hair don't go out, how be to return a responsibility?
Code to compile successfully, STMP service also opened, with the authorization code of the login, email is not sent out! Consult the friends in the BBS, grateful,

The following is a complete code:

The from smtplib import SMTP_SSL
From email. Mime. The text import MIMEText # building text version of the text
From email. Mime. Multipart import MIMEMultipart # construct
email subjectFrom email. Mime. Application import MIMEApplication # to add attachments
From the email. The header import header

Host_server='smtp.qq.com'
Sender='[email protected]'
The PWD='XXXXXXXX'
Receiver='[email protected]'

Mail_title='python testing mail' # mail title
Mail_content="life is too short, I use Python!" # email body

MSG=MIMEMultipart (#) initialization email subject
MSG=[" Subject "] Header (mail_title, 'utf-8') # building title
MSG=[" From "] sender # set the sender
MSG=[" To "] Header (receiver, 'utf-8') # set the recipient
MSG. Attach (MIMEText (mail_content, 'text', 'utf8)) # set text body
Attachment=MIMEApplication (open (' D:/123. TXT ', 'rb'), read ()) # rb mail in the form of binary open
Attachment. Add_header (' Conternt - Disposition ', 'attachment' filename='123. TXT) # in the mail head display is attachment
MSG. Attach (attachment) # accessories attached to the email
SMTP=SMTP_SSL (host_server)
SMTP. Login (sender, PWD)
SMTP. Quit ()

CodePudding user response:

I have just completed a, the skylight

CodePudding user response:

# generated email
My_email_from="ops report robot" # mailbox information
My_email_to='technology'
# mail title
My_emali_Subject="information gathering" + now_time
# attachment body
My_email_text="everybody respected leaders: \ n \ \ t \ \ t t t the attachment for the daily data report results, please find! Robot \ n \ n from statements "
# the attachment address
My__file_path='C:/Users/Administrator/Desktop/automation programming/' + workbook_new
# attachment name
My_msg=create_email (my_email_from my_email_to, my_emali_Subject, my_email_text, my__file_path, workbook_new)

Email #
My_sender="[email protected]"
My_password="XXXXXXXXXXX"
My_receiver="xxxxxxxxx.com", "aaaaaaaa.com", "bbbbbbbbbbbb.com", "cccccccccccc.com"]

Send email function called #
Send_mail (my_sender my_password, my_receiver my_msg)

Def create_email (emali_from, email_to email_Subject, email_text, file_path, send_file_name) :
# enter the sender's nickname, the recipient's nickname, subject, body, and the attachment address, name of accessories to generate an email
Message=MIMEMultipart ()
# the body in the form of text is inserted into the email
Message. The attach (MIMEText (email_text, 'plain', 'utf-8))
# generate the sender name (this to send mail has nothing to do)
Message [' from ']=Header (emali_from, 'utf-8)
# generate the recipient name (this has nothing to do with receiving mail also)
Message [' to ']=Header (email_to, 'utf-8)

The theme of the # generated email
Message [' Subject ']=Header (email_Subject, 'utf-8)

# to read the content of the attachment
Att1=MIMEText (open (file_path, 'rb'). The read (), 'base64', 'GBK')
Att1 [" the content-type "]='application/octet - stream'
The name of # generate attachment
# att1 [" Content - Disposition "]='attachment; Filename='+ send_file_name
Att1. Add_header (' Content - Disposition ', 'attachment' filename=send_file_name)
# insert the attachment in the email
Message. The attach (att1)
The return message

Def send_mail (sender, password, receiver, MSG) :
# an input email and password, the recipient, sent email function
Try:
# find your sending mail server address, in the form of encryption to send
Server=smtplib. SMTP_SSL (" smtp.qq.com ", 465) # in the sender email SMTP server
Server. The ehlo ()
# login to your account
Server. The login (sender, password) # corresponding in parentheses is the sender email accounts, passwords
Email #
Server. The sendmail (sender, receiver, MSG. As_string ())
# corresponding in parentheses is the sender's E-mail account, the recipient's email accounts (a list), email content
Print (" automatic email success...
")Server. The quit ()
Except the Exception as e:
Print (traceback. Print_exc ())
Print (" mail delivery failure... ")
  • Related