У меня работает django веб-сайт, который обслуживается IIS на экземпляре amazon ec2 (windows 10), и я использую модуль boto3 для отправки электронного письма.
Я установил awscli и запустил aws настроить и настроить мои 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',
Я использовал для отправки электронных писем, когда этот веб-сайт только работал с использованием " python manage.py runserver 0.0.0.0:80 "в CMD, но после того, как я развернул его в IIS, а также добавил SSL, он больше не работает.
Я нашел что-то похожее вопрос за apache, но не знаю, как приспособиться к моим обстоятельствам.