- Mais recentes
- Mais votos
- Mais comentários
Ok I have managed to solve this issue...
It has to do with the email envelope. The SENDER determines how the email is routed, the FROM is what the user see in the email. Below is the Python code that AWS has an example on their website to send via the SMTP API. I have adjusted it to have a a SENDER and a SENDER_FROM. With this code one can now send "On behalf" of a domain we do not own (but the email is verified) and ALSO detect via the SNS topic that the email has been sent successfully.
import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def lambda_handler(event, context):
SENDER = 'user1@X.com'
SENDER_FROM = 'user3.Z.com'
SENDERNAME = 'user3'
RECIPIENT = 'user2@Y.com'
USERNAME_SMTP = "YOUR_SMTP_USER"
PASSWORD_SMTP = "YOUR_SMTP_PASSWORD"
HOST = "YOUR_SMTP_HOST"
PORT = "YOUR_SMTP_PORT"
# The subject line of the email.
SUBJECT = 'Amazon SES Test (Python smtplib)'
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Amazon SES Test\r\n"
"This email was sent through the Amazon SES SMTP "
"Interface using the Python smtplib package."
)
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
<h1>Amazon SES SMTP Email Test</h1>
<p>This email was sent with Amazon SES using the
<a href='https://www.python.org/'>Python</a>
<a href='https://docs.python.org/3/library/smtplib.html'>
smtplib</a> library.</p>
</body>
</html>
"""
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg\['Subject'] = SUBJECT
msg\['From'] = email.utils.formataddr((SENDERNAME, SENDER_FROM))
msg\['To'] = RECIPIENT
# Comment or delete the next line if you are not using a configuration set
# msg.add_header('X-SES-CONFIGURATION-SET',CONFIGURATION_SET)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(BODY_TEXT, 'plain')
part2 = MIMEText(BODY_HTML, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Try to send the message.
try:
server = smtplib.SMTP(HOST, PORT)
server.ehlo()
server.starttls()
#stmplib docs recommend calling ehlo() before & after starttls()
server.ehlo()
server.login(USERNAME_SMTP, PASSWORD_SMTP)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
server.close()
# Display an error message if something goes wrong.
except Exception as e:
print ("Error: ", e)
else:
print ("Email sent!")
Edited by: TobieVdMerwe on Mar 28, 2019 6:41 AM
Conteúdo relevante
- AWS OFICIALAtualizada há 2 meses