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:

  1. How to force to send this html to draw this table in the letter directly?
  2. 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 ...
  3. 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!

  • Give the text of the error on the third. - mkkik
  • @mkkik joke is that there is no error. But if you send him ['a @ bc', 'x @ zy'] (That is, my_class (). Send_mail ('a @ bc, x @ zy'), then the letter comes only to the first. Format and type in both cases I’ve tried both with the list and with str and in "" I’m closing it - it’s not rolling. For some reason I’m rolling only if I split first, and then the join is sent to all recipients. I can’t understand why :( - user210429
  • 1. ['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 string recipients - is there an error here? - mkkik
  • You did not understand. And now, and if you unzip what is registered there - print and type msg ['To'] output the same thing. Therefore, I can not understand what the difference is ... PS If you 're interested, I took an example from here: stackoverflow.com/questions/8856117/… - user210429
  • The fact that the recipients line is now changed, and if you do it as in the commented line, it does not change. - mkkik

1 answer 1

The answer to the first question:

  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 rest are relevant ...

  • To supplement your question, please use the edit option. The “Post Reply” button should only be used for comprehensive answers to questions. - From the queue of checks - Vasily Barbashev
  • corrected. That's better? - user210429
  • Edit should be made to the question, not the answer. Write for example: 1 question was resolved like this: and add the code in a collapsed form (this is done by inserting a code snippet, and setting a check mark скрывать from below) - Vasily Barbashev
  • This is done by inserting a snippet of code, and from the bottom setting a tick to hide - I do not know how :( - user210429
  • The message control panel has: bold text, oblique text, link insertion, quote, code, picture, code snippet , ... lists. - Vasily Barbashev