我想使用gmail的smtp服务器发送电子邮件。我已正确设置gmail和send_mail()工作的设置变量。但是,当我尝试通过EmailMultiAlternatives发送功能发送电子邮件时,它将挂起而不发送。我的电子邮件代码如下。
d = Context(dict(users=users, frontdesk={'user': frontdesk, 'password': password}))
htmly = get_template('email.html')
subject, from_email, to = 'Account Info', 'myemail@gmail.com', ' myemail@gmail.com'
html_content = htmly.render(d)
text_content = "This is text content"
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, 'text/html')
print 'Trying to send email...'
msg.send()
这不起作用,但send_mail(subject, text_content, 'myemail@gmail.com', 'myemail@gmail.com')
可行。
我的设置:
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = '587'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'my-password'
EMAIL_USE_TLS = True
我做错了什么?我可以使用send_mail发送HTML电子邮件吗?我正在使用django 1.6.5。