你好?我正在使用Amazon SES通过Flask rq Queue发送电子邮件。问题是,如果我使用文档中的enqueue函数,则永远不会发送电子邮件,但是如果我使用参数调整函数以调用send_email,则会发送电子邮件,但应用程序将引发错误AttributeError:'bool '对象没有属性'模块'这是我的代码
任务代码(文件task.py,具有send_email函数)
def send_email(from_email, to_email, subject, body):
ses = boto3.client(
'ses',
region_name=os.getenv('SES_REGION'),
aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY')
)
ses.send_email(
Source=from_email,
Destination={'ToAddresses': [to_email]},
Message={
'Subject': {'Data': subject},
'Body': {
'Html': {'Data': body}
}
}
)
return True
a)可行但无法发送电子邮件的场景
# Queue confirmation email
try:
# Generate token, confirm url, and template
token = encode_token(email)
confirm_url = generate_url('guest.confirm_email', token)
body = render_template('emails/confirm.html', first_name=first, confirm_url=confirm_url)
#send_mail("email@domain.com", email, "Confirm your Email - Company Name", body)
# Enqueue
redis_url = os.getenv('REDIS_URL')
with Connection(redis.from_url(redis_url)):
q = Queue()
q.enqueue(send_email, "email@domain.com", email, "Confirm your email address", body)
return jsonify({'success': 1})
except IntegrityError:
db.session.rollback()
return jsonify({'success': 0})
通过上述操作,应用程序不会引发错误,不会返回成功代码,但是电子邮件永远不会发送
b)方案2(发送电子邮件,但应用程序抛出错误:AttributeError:'bool'对象没有属性' module ')
为此,我改为调用send_email方法并将参数作为方括号传递给
q.enqueue(send_email("email@domain.com", email, "Confirm your email address", body))
已发送电子邮件,但应用程序引发错误。 方案2的完整代码:
# Queue confirmation email
try:
# Generate token, confirm url, and template
token = encode_token(email)
confirm_url = generate_url('guest.confirm_email', token)
body = render_template('emails/confirm.html', first_name=first, confirm_url=confirm_url)
#send_mail("email@domain.com", email, "Confirm your Email - Company Name", body)
# Enqueue
redis_url = os.getenv('REDIS_URL')
with Connection(redis.from_url(redis_url)):
q = Queue()
q.enqueue(send_email("email@domain.com", email, "Confirm your email address", body))
return jsonify({'success': 1})
except IntegrityError:
db.session.rollback()
return jsonify({'success': 0})
任何帮助将不胜感激。