func:
def send_mail(self, recipients): import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart msg = MIMEMultipart('alternative') sender = 'my@mail' msg['Subject'] = "My subj" msg['From'] = sender recipients = recipients.split(",") # msg['To'] = ", ".join(recipients.split(",")) Почему, кстати, так не работает? msg['To'] = ", ".join(recipients) text = "Hi there! My table:" html = "Что тут написать, чтоб отправить html /tmp/report.html" part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') msg.attach(part1) msg.attach(part2) try: mail = smtplib.SMTP('my.mail.srv') mail.sendmail(sender, recipients, msg.as_string()) mail.quit() print "Successfully sent email" except Exception as err: print "Error: unable to send email: %s" % err HTML via /tmp/report.html:
<html> <body> <br></br> <FONT size=4><B>Hello!</B></FONT> <br></br> <table border=1> <tr> <td><B>ROW1</B></td> <td><B>ROW2</B></td> </tr> <tr> <td>ololo</td> <td>tralala</td> </tr> </table> <BR /> <FONT size=4><B> Bye! </B></FONT><BR> </B></FONT><BR /> </body> </html> A few questions:
- How to force to send this html to draw this table in the letter directly?
- How can one form it variable? Now I first make the file, then the script fills it with a bunch of ololo and tralala, then I assign the ending there and I need to send it. Can this be done not in a file, but in a variable? So it will probably be easier ...
- There it is written in the comments ...
Question 1 was decided as follows:
def send_mail(self, recipients): import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart msg = MIMEMultipart('alternative') sender = 'my@mail' msg['Subject'] = "My subj" msg['From'] = sender recipients = recipients.split(",") # msg['To'] = ", ".join(recipients.split(",")) Почему, кстати, так не работает? msg['To'] = ", ".join(recipients) text = "Hi there! My table:" html = open('/tmp/report.html', 'r').read() part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') msg.attach(part1) msg.attach(part2) try: mail = smtplib.SMTP('my.mail.srv') mail.sendmail(sender, recipients, msg.as_string()) mail.quit() print "Successfully sent email" except Exception as err: print "Error: unable to send email: %s" % err The remaining questions are still relevant!
['a@bc', 'x@zy']and 'a @ bc, x @ zy' are not the same. 2.mail.sendmail(sender, recipients, msg.as_string())- in this line you send the initial stringrecipients- is there an error here? - mkkikrecipientsline is now changed, and if you do it as in the commented line, it does not change. - mkkik