[英]EmailMultiAlternatives no connection could be made because the target machine actively refused it
I'm trying to make an ajax request to will generate a password for a user and send them an email with the password. 我正在尝试发出ajax请求,以为用户生成密码并向他们发送包含密码的电子邮件。 This all works fine, except for the error I'm getting at
msg.send()
除了我在
msg.send()
遇到的错误,所有这些都工作正常
Ajax: 阿贾克斯:
<script type="text/javascript">
var frm = $('#retrieveKeyForm');
frm.submit(function (e) {
e.preventDefault();
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
console.log('Submission was successful.');
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
},
});
});
</script>
Views.py Views.py
class GenerateSecretKey(APIView):
def get(self, request):
#Get email address from request
emailaddr = request.GET.get('email')
print(emailaddr)
#Show the email address(for debugging)
min_char = 10
max_char = 12
allchar = string.ascii_letters + string.digits
#Generate passsword
password = "".join(choice(allchar) for x in range(randint(min_char, max_char)))
print("Your password is {}".format(password))
subject, from_email, to = 'Your key is ready!', 'test@test.com', emailaddr
html_content = render_to_string('testapp/email.html', {'password':password})
text_content = strip_tags(html_content)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
return Response({'Success':'Your key has been generated. Please check your email.'})
Error: 错误:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
This block of code worked much better. 此代码块工作得更好。 I hadn't configured an SMTP server or specified any SMTP details so that's why I was getting that error.
我没有配置SMTP服务器或指定任何SMTP详细信息,所以这就是我收到该错误的原因。 Special thanks to this guy https://github.com/llamafarmer/email/blob/master/sendEmailAttachment.py
特别感谢这个家伙https://github.com/llamafarmer/email/blob/master/sendEmailAttachment.py
fromaddr = "test@test.com"
toaddr = request.GET.get('email')
print(toaddr)
smtp_user = "test@gmail.com"
smtp_pass = "abctest"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Ready!"
body = render_to_string('testapp/email.html', {'password':password})
msg.attach(MIMEText(body, 'html'))
server = smtplib.SMTP('in-v3.mailjet.com', 587)
server.starttls()
server.login(smtp_user, smtp_pass)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
return Response({'Success':'Your key has been generated. Please check your email.'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.