Detecting bounced and delivered emails that were sent "On behalf"

0

Hi,

I have a problem that I hope someone can steer me in the correct direction please.

We have setup a SES server and have configured it to have a custom domain for example X.com
There is no issue when we send for example emails FROM user1@X.com TO user2@Y.com . The user2 get the email in his inbox and we have setup SNS SES topics to detect if it was successful or if the email has been bounced. So this use case is covered.

However as this is a type of B2B system that we are building so the requirement has now come up where it is now required to send "On behalf" of another domain for example Z.com that we do not own, but we can get the sender email verified in SES to be able to use it. The customer want to do it so that it looks like the email came from his domain Z.com instead of our domain X.com.
So when we send now an email FROM user3.Z.com TO user2@Y.com via SES using the SMTP interface using a Lambda (python language used). The email is send to the the users email box without an issue. However we are not able to detect the bounce OR success using the SNS topics in SES. It is this inability to detect the success or bounce that is a big problem for us. I have played around Return Path, but SES overwrites it. I have tried to set several other email headers, but it is not working. I have also played around by setting the MAIL TO domain, but it is not solving the issue.

Is there anyone that could maybe tell be if it is possible to detect the bounce or success for emails that is send "On Behalf" of another domain?

Thanks
Tobie van der Merwe

asked 5 years ago707 views
1 Answer
0

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

answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions