Search code examples
emailsmtpattachmentaws-gluemime

Mail not getting sent through AWS Glue Python job


I am trying to send a mail through an AWS Glue job. The mail will have multiple number of attachments that it gets from the s3 bucket. According to the logs, it is running until server.login(). It is failing in the server.sendmail() function. Following is the code -

def sendEmail(TO, SUBJECT, BODY_HTML):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = SUBJECT
    msg['From'] = SENDER
    msg['To'] = ','.join(RECIPIENT + TO)
    part1 = MIMEText(BODY_HTML, 'html')
    msg.attach(part1)
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('sample-bucket')
    for obj in bucket.objects.filter(Delimiter='/', Prefix='sample-folder/'):
        filename =  ((obj.key).split("/")[1])
        s3_object = s3_obj.s3_get_object(sample-bucket, 'sample-folder/'+ filename)
        body = s3_object['Body'].read()
        part = MIMEApplication(body, filename)
        part.add_header("Content-Disposition", 'attachment', filename=filename)
        msg.attach(part)
    try:
        server = smtplib.SMTP(HOST, PORT)
        server.ehlo()
        server.starttls()
        server.ehlo()
        server.login(USERNAME_SMTP, PASSWORD_SMTP)
        server.sendmail(SENDER, RECIPIENT, msg.as_string()) ***--Error***
        server.close()
        print (msg)
        print ("Email sent")

I am getting the following error -

Error: (554, b'Transaction failed: Expected '=', got "null"')

What is the issue?


Solution

  • I got the answer. The problem was with the way files were read from s3. The output of the first iteration was -

    sample-bucket/sample-folder/

    So, it was taking a null object and failing. So, I just skipped the first object in the iteration and carried out the whole thing. It worked. Please find the final code below -

    def sendEmail(TO, SUBJECT, BODY_HTML):
        msg = MIMEMultipart('alternative')
        msg['Subject'] = SUBJECT
        msg['From'] = SENDER
        msg['To'] = ','.join(RECIPIENT + TO)
        part1 = MIMEText(BODY_HTML, 'html')
        msg.attach(part1)
        s3 = boto3.resource('s3')
        bucket = s3.Bucket('sample-bucket')
        **it = iter(bucket.objects.filter(Delimiter='/', Prefix='sample-folder/'))
        next(it, None)
        for obj in it:**
            filename =  ((obj.key).split("/")[1])
            s3_object = s3_obj.s3_get_object(sample-bucket, 'sample-folder/'+ filename)
            body = s3_object['Body'].read()
            part = MIMEApplication(body, filename)
            part.add_header("Content-Disposition", 'attachment', filename=filename)
            msg.attach(part)
        try:
            server = smtplib.SMTP(HOST, PORT)
            server.ehlo()
            server.starttls()
            server.ehlo()
            server.login(USERNAME_SMTP, PASSWORD_SMTP)
            server.sendmail(SENDER, RECIPIENT, msg.as_string()) 
            server.close()
            print (msg)
            print ("Email sent")