In the below lambda function, I want to pass "Message2" variable in the HTML "BODY_HTML". Is there a way to pass my event Message2 in the HTML.
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
Message1 = event ["Message"]
print("Message_Received : ")
print(Message1)
Message2 = event ["Event"]
print("Event_Received : ")
print(Message2)
Message3 = event ["Sender"]
print("Sender : ")
print(Message3)
SENDER = "Redshift Pause Resuem Service <redshift#abc.com>"
RECIPIENT = Message3
#CONFIGURATION_SET = "ConfigSet"
AWS_REGION = "us-east-1"
SUBJECT = subject1
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
"This email was sent with Amazon SES using the "
"AWS SDK for Python (Boto)."
)
BODY_HTML = **"""<html>
<head></head>
<body>
<p>I want to print here the variable "Message2"</p>
</body>
</html>
"""**
The character encoding for the email.
CHARSET = "UTF-8"
Create a new SES resource and specify a region.
client = boto3.client('ses',region_name=AWS_REGION)
Try to send the email.
try:
#Provide the contents of the email.
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
Try using string formatting:
BODY_HTML = f"""<html>
<head></head>
<body>
<p>{Message2}</p>
</body>
</html>
"""
Or string concatenation:
BODY_HTML = f"<html><head></head><body><p>" + Message2 + "</p></body></html>"
Related
I ran the following script which works on my computer but doesnt work in AWS Lambda. All I did was added "def lambda_handler(event, context): return event" function as its required by the lambda. I ran the Lambda test a few times.
I am not getting the SES email and there's no errors when I execute it in Lambda. Any idea why?
import boto3
from botocore.exceptions import ClientError
SENDER = "Sender Name <testu#test.com>"
RECIPIENT = "test#test.com"
CONFIGURATION_SET = "ConfigSet"
AWS_REGION = "ap-southeast-2"
SUBJECT = "Amazon SES Test (SDK for Python)"
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
"This email was sent with Amazon SES using the "
"AWS SDK for Python (Boto)."
)
BODY_HTML = """<html>
</html>
"""
def lambda_handler(event, context):
return event
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES resource and specify a region.
client = boto3.client('ses',region_name=AWS_REGION)
try:
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
ConfigurationSetName=CONFIGURATION_SET,
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print(response['MessageId'])
Just like what Anon Coward said, you have to perform the SES sending inside the handler function and put the return statement at the bottom of that function.
It should look something like this:
def lambda_handler(event, context):
response = client.send_email(PAYLOAD_HERE_)
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Region ": json_region
})
}
I have a python script that pulls customer transaction data from our DB and formats it to be used by SendGrid's Dynamic Templates, but I cannot figure out from the SendGrid docs or any other source how to insert each customer's unique data in their email. Here is the relevant code from the script (assume all variables are pre-filled by the script):
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, To
message = Mail(
from_email=("no_reply#mywebsite.com", "My Website"),
to_emails=to_emails,
is_multiple=True)
message.template_id = "d-thetemplateidthatiamusing"
try:
sendgrid_client = SendGridAPIClient("SG.thesecretkeythatdoesnotbelongonstack")
sendgrid_client.send(message)
except Exception as e:
print(e)
When I run this, it sends to the correct customer emails, but without any custom data. I have the following object ready to send over for each customer, but have no idea how to attach it on an email by email basis:
{
"name": "Customer Name",
"games": [
{
"game_title": "Test Team vs. Demo Team",
"event_name": "Live Showcase I",
"date": "5/9/2021",
"score": "79-55",
"url": "app.website.com/"
},
{
"game_title": "Test Team vs. Demo Team",
"event_name": "Live Showcase I",
"date": "5/9/2021",
"score": "79-69",
"url": "app.website.com/"
}
]
}
When I throw this JSON in the SendGrid template test data area it works perfectly with my design. Any thoughts?
You are missing an attribute:
message.dynamic_template_data = {
"name": "Customer Name",
}
To pass information dynamically you need to pass an instance of the Customer to your email dispatcher:
def sendEmail(instance):
message = Mail(
from_email=("no_reply#mywebsite.com", "My Website"),
to_emails=to_emails,
is_multiple=True)
message.dynamic_template_data = {
"name":instance.name
}
message.template_id = "d-thetemplateidthatiamusing"
try:
sendgrid_client = SendGridAPIClient("SG.thesecretkeythatdoesnotbelongonstack")
sendgrid_client.send(message)
except Exception as e:
print(e)
You did not include your initialization code for to_emails,
here is a link to an example that shows how to set dynamic template data for each 'to' email address. Also, it wasn't clear to me, does each recipient have their own set of json data, or is name the only thing difference between them? In other words, would they all be receiving the same data for games?
And here is an excerpt (adjust the recipient and dynamic_template_data to fit your needs):
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, To
to_emails = [
To(email='test+to0#example.com',
name='Customer Name 0',
dynamic_template_data={
'name': 'Customer Name 0',
'games': games,
},
subject='Override Global Subject'),
To(email='test+to1#example.com',
name='Customer Name 1',
dynamic_template_data={
'name': 'Customer Name 1',
'games': games,
}),
]
message = Mail(
from_email=('test+from#example.com', 'Example From Name'),
to_emails=to_emails,
subject='Global subject',
is_multiple=True)
message.template_id = 'd-12345678901234567890123456789012'
try:
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
just to add to what #jacko_iced_out answered, in SendGrid you should wrap the variable you want to set with {{}}
<div class="v-font-size" style="color: #444444; line-height: 170%; text-align: center; word-wrap: break-word;">
<p style="font-size: 14px; line-height: 170%;">
<span style="font-size: 20px; line-height: 34px;">
<strong>
<span style="line-height: 34px;">{{password}}</span>
</strong>
</span>
</p>
</div>
And here is an example of python code to send mail
from fastapi import HTTPException
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from config import settings
async def sendLoginMailClient(admin_mail, client_mail, password):
message = Mail(
subject='Invitation de création de maturité numirique profil',
from_email=("from_sendgridmail_mail"),
to_emails=client_mail
)
message.dynamic_template_data = {
"admin": admin_mail,
'client': client_mail,
"password": password
}
message.template_id = "d-c9f1894881ff426aa614e5c41e831f16"
try:
sg = SendGridAPIClient(settings.sendgrid_api_key)
response = sg.send(message)
code = response.status_code
return {
'success': True,
'data': code
}
except Exception as e:
raise HTTPException(
status_code=500,
detail=f'error:{e}'
)
For more detail check this link https://www.twilio.com/blog/send-dynamic-emails-python-twilio-sendgrid
def getbodyinbox():
service = build('gmail', 'v1', credentials=creds)
label_name = "READ-BY-SCRIPT"
label_id = 'Label_8507504117657095973'
results = service.users().messages().list(
userId='me', q="-label:"+label_name, maxResults=1).execute()
messages = results.get('messages', [])
body = []
if not messages:
body = "no messages"
return body
else:
for message in messages:
msg = service.users().messages().get(
userId='me', id=message['id']).execute()
labels = msg['labelIds']
if "INBOX" in labels:
headers = msg['payload']['headers']
headers = str(headers)
print(headers)
if "class_ix" in headers:
body.append(msg['payload']['parts'])
if 'data' in body[0][0]['body']:
body = base64.urlsafe_b64decode(
body[0][0]['body']['data'])
elif 'data' in body[0][1]['body']:
body = base64.urlsafe_b64decode(
body[0][1]['body']['data'])
body = str(body)
return body
print(getbodyinbox())
This is my code so far with the parts that get the credentials removed. It gets the body of the most recent email without a label 'READ-BY-SCRIPT' that also has the label INBOX. How can I adapt it to get the sender of the email?
You can get the sender of an email in Message -> payload -> headers
You just need to match the header['name'] == 'From' and gets its equivalent header['value']
Sample Message Response Body (for Headers):
{
"payload": {
"partId": "",
"mimeType": "multipart/alternative",
"filename": "",
"headers": [
{
"name": "From",
"value": "User \u003cuser#email.com\u003e"
},
.....
{
"name": "Subject",
"value": "TEST EMAIL"
},
],
}
}
Sample Code:
headers = msg['payload']['headers']
for header in headers: # getting the Sender
if header['name'] == 'From':
msg_from = header['value']
break
Note:
Header sender value has this kind of format: "User \u003cuser#email.com\u003e"
You can get the sender's name by parsing the string from the start until \u003c tag, while you can get the sender's email by parsing the string that is enclosed in \u003c and \u003e tags.
I have errors when I use reply_to, here is what I've tried...
This works but without reply_to :
mail = Mail(
from_email = from_email,
to_emails = to_email,
subject = subject,
html_content = content)
response = sg.client.mail.send.post(request_body=mail.get())
And I've tried this :
mail = Mail(
reply_to = reply,
from_email = from_email,
to_emails = to_email,
subject = subject,
html_content = content)
response = sg.client.mail.send.post(request_body=mail.get())
This thows :
TypeError: init() got an unexpected keyword argument 'reply_to'
I have also tried to add this after Mail() :
mail.from_EmailMessage(reply_to=ReplyTo(Email(email=ANS_EMAIL,name='Mr X')))
Which throws :
TypeError: object of type 'Email' has no len()
And :
mail.reply_to(ReplyTo(email=ANS_EMAIL,name='Mr X'))
Which throws :
TypeError: 'NoneType' object is not callable
Any help please ?
As your error states reply_to is not accepted in init(). Try setting reply_to after you have created the Mail object.
Example Below:
mail.reply_to = "reply-to#website.com"
or
message.reply_to = ReplyTo('reply-to#website.com', 'Reply Here')
More details on how to customize sendgrid email object - Link
I managed doing this :
data = {
"personalizations": [
{
"send_at": date,
"subject": objet,
"to": [{"email": to_email}],
}
],
"from": {"email": FROM_EMAIL, "name": "..."},
"reply_to": {"email": "...#...", "name": "..."},
"send_at": date,
"subject": objet,
"content": [
{
"type": "text/html",
"value": body
}
]
}
response = sg.client.mail.send.post(request_body=data)
I am trying to get access to the Kerio Connect (mailserver) api which uses jsonrpc as a standard for their api.
There is Session.login method that works just fine, I get back a SESSION_CONNECT_WEBADMIN cookie that gets saved in the session:
SESSION_CONNECT_WEBADMIN=2332a56d0203f27972ebbe74c09a7f41262e5b224bc6a05e53e62e5872e9b698; \
path=/admin/; domain=<server>; Secure; HttpOnly; Expires=Tue, 19 Jan 2038 03:14:07 GMT;
But when I then do my next request with the same session, I get back a message that tells me, that my session has expired:
{
"jsonrpc": "2.0",
"id": 2,
"error": {
"code": -32001,
"message": "Session expired.",
"data": {
"messageParameters": {
"positionalParameters": [],
"plurality": 1
}
}
}
}
So here's the Python script leading to that message
import json
import requests
userName = "username"
password = "password"
n=1
application = {}
application["name"] = "Log in"
application["vendor"] = "My Company"
application["version"] = "1.0"
params = {}
params["userName"] = userName
params["password"] = password
params["application"] = application
payload = {}
payload["jsonrpc"] = "2.0"
payload["id"] = n
n += 1
payload["method"] = "Session.login"
payload["params"] = params
headers = {}
headers["Content-Type"] = "application/json-rpc"
json_payload =json.dumps(payload, sort_keys=True, indent=2)
url = "https://<server>:4040/admin/api/jsonrpc/"
session = requests.Session()
response = session.post(url, headers=headers, data=json_payload, verify=False)
# Results in a token / a cookie with that token
payload2 = {}
payload2["jsonrpc"] = "2.0"
payload2["id"] = n
n += 1
payload2["method"] = "Users.get"
json_payload2 = json.dumps(payload2, sort_keys=True, indent=2)
response2 = session.post(url, data=json_payload2, verify=False)
print(response2.text)
What am I missing here because of my lack of experience?
[EDIT]:
I just now realise that when I log in with a browser, two cookies are actually created, each with another token, whereas I get only one cookie back when I try to access the api with Python. Why is that?
Cookies received with Chrome:
TOKEN_CONNECT_WEBADMIN
SESSION_CONNECT_WEBADMIN
Cookie received with Python:
SESSION_CONNECT_WEBADMIN
Working example:
import json
import urllib.request
import http.cookiejar
import ssl
jar = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
urllib.request.install_opener(opener)
server = "https://mail.smkh.ru:4040"
username = "admin"
password = "pass"
ssl._create_default_https_context = ssl._create_unverified_context # disable ssl cert error
def callMethod(method, params, token=None):
"""
Remotely calls given method with given params.
:param: method string with fully qualified method name
:param: params dict with parameters of remotely called method
:param: token CSRF token is always required except login method. Use method "Session.login" to obtain this token.
"""
data = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params}
req = urllib.request.Request(url=server + '/admin/api/jsonrpc/')
req.add_header('Content-Type', 'application/json')
if token is not None:
req.add_header('X-Token', token)
httpResponse = opener.open(req, json.dumps(data).encode())
if httpResponse.status == 200:
body = httpResponse.read().decode()
return json.loads(body)
session = callMethod("Session.login", {"userName": username, "password": password, "application": {"vendor":"Kerio", "name":"Control Api Demo", "version":"8.4.0"}})
token = session["result"]["token"]
sessions = callMethod("Users.get",
{"query": {
"fields": [
"id",
"loginName",
"fullName",
"description",
"authType",
"itemSource",
"isEnabled",
"isPasswordReversible",
"emailAddresses",
"emailForwarding",
"userGroups",
"role",
"itemLimit",
"diskSizeLimit",
"consumedItems",
"consumedSize",
"hasDomainRestriction",
"outMessageLimit",
"effectiveRole",
"homeServer",
"migration",
"lastLoginInfo",
"accessPolicy"
],
"start": 0,
"limit": 200,
"orderBy": [
{
"columnName": "loginName",
"direction": "Asc"
}
]
},
"domainId": Example:"keriodb://domain/908c1118-94ef-49c0-a229-ca672b81d965"
},
token)
try:
user_names = []
for user in users["result"]["list"]:
print(user["fullName"], " (", user["loginName"], ")", sep="")
user_names.append(user["fullName"])
call_method("Session.logout", {}, token)
return users
except KeyError:
print('Error: {}'.format(users['error']['message']))
call_method("Session.logout", {}, token)
return None