smtp - python email sent and received with smtplib has no content -


i sending email raspberrypi python. sent , received message, content missing.

here code

import smtplib  smtpuser = 'myemail@gmail.com' smtppass = 'mypassword'  toadd = 'target@gmail.com' fromadd = smtpuser  subject = 'python test' header = 'to: ' + toadd + '\n' + 'from: ' + fromadd + '\n' + 'subject: ' +             subject body = 'from within python script'  msg = header + '\n' + body  print header + '\n' + body  s = smtplib.smtp('smtp.gmail.com',587)  s.ehlo() s.starttls() s.ehlo()  s.login(smtpuser,smtppass) s.sendmail(fromadd, toadd, msg)  s.quit() 

thank attention!

i wrote wrapper around sending emails in python; makes sending emails super easy: https://github.com/krystofl/krystof-utils/blob/master/python/krystof_email_wrapper.py

use so:

emailer = krystof_email_wrapper()  email_body = 'hello, <br /><br />' \              'this body of email.'  emailer.create_email('sender@example.com', 'sender name',                      ['recipient@example.com'],                      email_body,                      'email subject!',                      attachments = ['filename.txt'])  cred = { 'email': 'sender@example.com', 'password': 'verysecure' }  emailer.send_email(login_dict = cred, force_send = true) 

you can @ source code find how works. relevant bits:

import email import smtplib   # sending of emails email.mime.multipart   import mimemultipart email.mime.text        import mimetext  self.msg = mimemultipart()  self.msg.preamble   = "this multi-part message in mime format."  # set sender author = email.utils.formataddr((from_name, from_email)) self.msg['from'] = author  # set recipients (from list of email address strings) self.msg['to' ] = ', '.join(to_emails) self.msg['cc' ] = ', '.join(cc_emails) self.msg['bcc'] = ', '.join(bcc_emails)  # set subject self.msg['subject'] = subject  # set body msg_text = mimetext(body.encode('utf-8'), 'html', _charset='utf-8') self.msg.attach(msg_text)  # send email session = smtplib.smtp('smtp.gmail.com', 587) session.ehlo() session.starttls() session.login(login_dict['email'], login_dict['password']) session.send_message(self.msg) session.quit() 

Comments