2016-03-26 24 views
1

python ile birlikte raspberrypi ile e-posta gönderiyorum. İletiyi başarıyla gönderdim ve aldım, ancak içerik eksik.python eposta ile gönderilen ve alınan smtplib hiçbir içeriğe sahip değil.

İşte

import smtplib 

smtpUser = '[email protected]' 
smtpPass = 'mypassword' 

toAdd = '[email protected]' 
fromAdd = smtpUser 

subject = 'Python Test' 
header = 'To: ' + toAdd + '\n' + 'From: ' + fromAdd + '\n' + 'Subject: ' +    subject 
body = 'From within a 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() 

İlginiz için teşekkür ederiz benim kodudur!

cevap

0

Python'a e-posta göndermenin etrafında bir sarıcı yazdım; süper kolay e-posta göndermek yapar: https://github.com/krystofl/krystof-utils/blob/master/python/krystof_email_wrapper.py

şöyle kullanın:

emailer = Krystof_email_wrapper() 

email_body = 'Hello, <br /><br />' \ 
      'This is the body of the email.' 

emailer.create_email('[email protected]', 'Sender Name', 
        ['[email protected]'], 
        email_body, 
        'Email subject!', 
        attachments = ['filename.txt']) 

cred = { 'email': '[email protected]', 'password': 'VERYSECURE' } 

emailer.send_email(login_dict = cred, force_send = True) 

Ayrıca nasıl çalıştığını bulmak için kaynak koduna bakabilirsiniz. İlgili bitler:

import email 
import smtplib # sending of the emails 
from email.mime.multipart import MIMEMultipart 
from email.mime.text  import MIMEText 

self.msg = MIMEMultipart() 

self.msg.preamble = "This is a multi-part message in MIME format." 

# set the 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 the subject 
self.msg['Subject'] = subject 

# set the body 
msg_text = MIMEText(body.encode('utf-8'), 'html', _charset='utf-8') 
self.msg.attach(msg_text) 

# send the 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() 
İlgili konular