class Message(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=20)
text = models.TextField()
has_been_sent = models.BooleanField(default=False)
def test(self):
self.name = u'Имя'
self.phone = u'phone 123123'
self.email = u'email dasdas'
self.text = u'text sdadasdasdasd'
self.send_email()
def send_email(self):
fromaddr = 'no-reply@my-site.ru'
toaddrs = 'toaddr@mail.ru'
body = smart_text(u'Телефон: "{}"\nСообщение:\n"{}"'.format(self.phone, self.text))
msg = MIMEText(body, _charset='utf-8')
msg['Subject'] = smart_text(u"Новое сообщение. '{}'".format(self.name))
msg['From'] = fromaddr
msg['To'] = toaddrs
username = 'no-reply@my-site.ru'
password = 'password'
try:
server = smtplib.SMTP_SSL(u'smtp.fullspace.ru:465') # замени на нужный сервер.
server.login(username, password)
server.sendmail(fromaddr, [toaddrs], msg.as_string())
except smtplib.SMTPAuthenticationError as e:
print(e)
except smtplib.SMTPException as e:
print(e)
else:
self.has_been_sent = True
finally:
server.quit()
def __unicode__(self):
return u'{} ({})'.format(self.name, self.email)