1

I am new to AWS Lambda function.

I wanted to send email to multiple recipients. I am able to send email to single email address but not multiple email ids and shows error.

I just refered the amazon documentation page and wrote the below code.

I am using environmental variable runteam and it has values like ['aaa@xyz.com','bbb@xyz.com','ccc@xyz.com']

import boto3
import os
import os, sys, subprocess
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

def lambda_handler(event,context):

    ses = boto3.client("ses")
    s3 = boto3.client("s3")
    runemail = os.environ['runteam']
    for i in event["Records"]:
        action = i["eventName"]
        #ip = i["requestParameters"]["soruceIPAddress"]
        bucket_name = i["s3"]["bucket"]["name"]
        object = i["s3"]["object"]["key"]

    fileObj = s3.get_object(Bucket = bucket_name, Key = object)
    file_content = fileObj["Body"].read()

    sender = "test@xyz.com"
    to = runemail
    subject = str(action) + 'Event from ' + bucket_name 

    body = """
        <br>
        This email is to notify regarding {} event
        This object {} is created
    """.format(action,object)

    msg = MIMEMultipart('alternative')

    msg["Subject"] = subject
    msg["From"] = sender
    msg["To"] = ', '.join(runemail)

    body_txt = MIMEText(body, "html")
    attachment = MIMEApplication(file_content)
    attachment.add_header("Content-Disposition","attachment", filename = "ErrorLog.txt")
    msg.attach(body_txt)
    msg.attach(attachment)
    response = ses.send_raw_email(Source = sender, Destinations = rumemail, RawMessage = {"Data": msg.as_string()})
    return "Thanks"
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Karthik
  • 85
  • 2
  • 9
  • What is the error you receive? How are you passing the list of addresses? The `runemail = os.environ['runteam']` line suggests it should be coming in via an Environment Variable which is a string, yet the line `msg["To"] = ', '.join(runemail)` suggests it should be coming in a List. Something doesn't match there. – John Rotenstein Jun 24 '19 at 05:39
  • Yes runteam has value like ['aaa@xyz.com','bbb@xyz.com','ccc@xyz.com'] Error: [ERROR] ClientError: An error occurred (InvalidParameterValue) when calling the SendRawEmail operation: Illegal address – Karthik Jun 24 '19 at 05:49
  • What is it EXACTLY? Does it really contain the square brackets and single quotes? It would just be a single string, so any such characters would be _inside_ the string. Are you able to change the format of that Environment Variable data? – John Rotenstein Jun 24 '19 at 05:52
  • I want to keep multiple emails in that environment variable runteam and i need to use that inside lambda. How to use it? That is my exact question. I checked in someother pages, they haved used square bracket to have multiple emails in a string. – Karthik Jun 24 '19 at 05:55
  • 1
    https://stackoverflow.com/questions/29660366/boto-ses-send-raw-email-to-multiple-recipients checkout this link – soheshdoshi Jun 24 '19 at 06:01
  • 1
    Yes, but that is Python code, which is defining a list. However, the environment variable is a single string, not a List of strings. – John Rotenstein Jun 24 '19 at 06:14
  • 1
    By the way, something looks strange with your `for` loop (or with the indentation of the code). It is looping through each Record, and extracting the bucket and object name, but then not doing anything with them. Thus, it will only extract the value of the _last_ Record. – John Rotenstein Jun 24 '19 at 06:16
  • @soheshdoshi thanks for the link. Yes it is working. But i want to keep the recipients list in the environmental variable. How to do that? I did the same way, but it show error Consider my environmental variable is runteam = ['aaa@xyz.com', 'bbb@xyz.com'] Error: Invalid type for parameter Destinations, value: ['aaa@xyz.com', 'bbb@xyz.com'], type: , valid types: , – Karthik Jun 24 '19 at 06:24
  • @Karthik use enum concept create dict strucure i hope it will help. – soheshdoshi Jun 24 '19 at 10:42

3 Answers3

3

I think everything seems to be right regarding the email sending code. The error lies in your program where the way you store your environ variable.

It should be stored as runteam="aaa@xyz.com bbb@xyz.com ccc@xyz.com" (notice the space between each email)

Then use this variable as
rumemail = os.environ['runteam'].split()

msg["To"] = ', '.join(runemail)

response = ses.send_raw_email(Source = sender, Destinations = rumemail, RawMessage = {"Data": msg.as_string()})

Anil Koppula
  • 743
  • 6
  • 11
2

This line:

msg["To"] = ', '.join(runemail)

is expecting a Python list, not a string. I suggest you add a debug line after it to see what you are actually sending the system.

I would recommending passing your environment variable as:

person@address.com, person2@address.com, person3@address.com

Then, use:

msg["To"] = runemail
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
0

Removing/commenting out the following line should fix the issue:

msg["To"] = ', '.join(runemail)

By the above line, you are converting a list to a string. However, Destinations attribute is looking for a list.

Notonb
  • 71
  • 2