简体   繁体   中英

How to remove excess syntax from email Python?

I am writing a script that sends new Usernames and Passwords to people using gmail in Python. The problem is that excess syntax appears in the message, which is not what I want.

#Sending Email containing new Username and Password
            Sub=("Password Change Request")
            Msg=("Hello, you have requested a Username and Password change."
                 "Your new credentials are Username: ",Username,"Password: ",Password,"")
            REmail=EmailF.get()
            try:
                server = smtplib.SMTP('smtp.gmail.com:587')
                server.ehlo()
                server.starttls()
                server.login(config.EMAIL_ADDRESS, config.PASSWORD)
                message = "Subject: {} \n\n {}".format(Sub, Msg)
                server.sendmail(config.EMAIL_ADDRESS, REmail, message)
                server.quit()
                print("Success!")

The variables Username and Password are random strings of characters. An example of the email produced can be seen below:

('Hello, you have requested a Username and Password change.Your new credentials are Username: ',
 'foo', 'Password: ', 'bar', '')

It should read: Hello, you have requested a Username and Password change. Your new credentials are Username: Username Password: Password

when doing

message = "Subject: {} \n\n {}".format(Sub, Msg)

you're passing Msg to str.format . Msg is a tuple , so str.format tries to convert it to a representable/printable form for debug purposes. Yes, you don't want to send a mail with that.

Instead, create Msg as a string , also using str.format :

Msg="Hello, you have requested a Username and Password change.\nYour new credentials are Username: {}, Password: {}".format(Username,Password)

If the string is too long as is you can cut it and add parentheses, but don't put any commas (as the tuple will return to bite you)

Msg=("Hello, you have requested a Username and Password change.\n"
 "Your new credentials are Username: {}, Password: {}").format(Username,Password)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM