【发布时间】:2021-06-24 20:47:26
【问题描述】:
我正在运行 django 网站,该网站由 IIS 在亚马逊 ec2 实例(Windows 10)上提供服务,并且我正在使用 boto3 模块发送电子邮件。
我安装了 awscli 并运行了 aws configure 并设置了我的 aws 访问密钥
我的电子邮件发送代码如下所示:
import boto3
from botocore.exceptions import ClientError
from django.shortcuts import redirect, render
from django.http import HttpResponseRedirect
def sendEmail(request):
if request.method == 'POST':
SENDER = "Sender Name <xxx>"
RECIPIENT = "xxx"
AWS_REGION = "eu-west-1"
# The subject line for the email.
SUBJECT = "Amazon SES Test (SDK for Python)"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
"This email was sent with Amazon SES using the "
"AWS SDK for Python (Boto)."
)
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
<h1>Amazon SES Test (SDK for Python)</h1>
<p>This email was sent with
<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
<a href='https://aws.amazon.com/sdk-for-python/'>
AWS SDK for Python (Boto)</a>.</p>
</body>
</html>
"""
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES resource and specify a region.
client = boto3.client('ses', region_name=AWS_REGION)
try:
# Provide the contents of the email.
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
return render(request, 'error.html')
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
return render(request, 'success.html')
其中 xxx 表示 AWS 上经过验证的电子邮件。
我的错误如下所示:
Django Version: 3.0.3
Exception Type: NoCredentialsError
Exception Value:
Unable to locate credentials
Exception Location: c:\users\administrator\python38\lib\site-packages\botocore\auth.py in add_auth, line 357
Python Executable: c:\users\administrator\python38\python.exe
Python Version: 3.8.1
Python Path:
['.',
'c:\\users\\administrator\\python38\\python38.zip',
'c:\\users\\administrator\\python38\\DLLs',
'c:\\users\\administrator\\python38\\lib',
'c:\\users\\administrator\\python38',
'c:\\users\\administrator\\python38\\lib\\site-packages',
当这个网站刚刚在 CMD 中使用“python manage.py runserver 0.0.0.0:80”运行时,我曾经发送电子邮件,但在我将它部署到 IIS 并添加 SSL 之后,它就不再工作了。
我为 apache 找到了类似的 question,但不知道如何适应我的情况。
【问题讨论】:
标签: django amazon-web-services boto3 iis-10