fork download
  1. class Message(models.Model):
  2.  
  3. name = models.CharField(max_length=100)
  4. email = models.CharField(max_length=255)
  5. phone = models.CharField(max_length=20)
  6. text = models.TextField()
  7. has_been_sent = models.BooleanField(default=False)
  8.  
  9. def test(self):
  10. self.name = u'Имя'
  11. self.phone = u'phone 123123'
  12. self.email = u'email dasdas'
  13. self.text = u'text sdadasdasdasd'
  14. self.send_email()
  15.  
  16. def send_email(self):
  17. fromaddr = 'no-reply@my-site.ru'
  18. toaddrs = 'toaddr@mail.ru'
  19.  
  20. body = smart_text(u'Телефон: "{}"\nСообщение:\n"{}"'.format(self.phone, self.text))
  21. msg = MIMEText(body, _charset='utf-8')
  22. msg['Subject'] = smart_text(u"Новое сообщение. '{}'".format(self.name))
  23. msg['From'] = fromaddr
  24. msg['To'] = toaddrs
  25.  
  26. username = 'no-reply@my-site.ru'
  27. password = 'password'
  28.  
  29. try:
  30. server = smtplib.SMTP_SSL(u'smtp.fullspace.ru:465') # замени на нужный сервер.
  31. server.login(username, password)
  32. server.sendmail(fromaddr, [toaddrs], msg.as_string())
  33. except smtplib.SMTPAuthenticationError as e:
  34. print(e)
  35. except smtplib.SMTPException as e:
  36. print(e)
  37. else:
  38. self.has_been_sent = True
  39. finally:
  40. server.quit()
  41.  
  42. def __unicode__(self):
  43. return u'{} ({})'.format(self.name, self.email)
Runtime error #stdin #stdout #stderr 0.02s 8736KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
NameError: name 'models' is not defined