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.
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
})
}
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>"
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 have a file that is connected to another one of my programs, and upon being run, it sends an embed to Discord using the following code:
payload = {"embeds": [
{
"author": {
"name": "EAS Software",
"icon_url": "https://www.fema.gov/sites/default/files/styles/unicorn_rotator/public/uni-rotator-imgs/eas_logo_rev1_3.png?itok=56iK9r2a"
},
"title": lines,
"description": "Issued "+datetime.now().strftime('%m/%d/%Y %H:%M:%S') + " " + TMZ,
"color": 0xff9900,
"fields": [
{
"name": "Text Data:",
"value": args.text
},
{
"name": "EAS Protocol Data:",
"value": "ZCZC-"+args.org+"-"+args.event+"-"+args.fips+"+"+args.time+"-"+ts_val+"-"+args.calls+"-"
},
],
"timestamp" : datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.000Z'),
"footer": {
"text": "Nerp EAS Logger System"
}
}
]
}
header_data = {'content-type': 'application/json'}
requests.post(webhook1, json.dumps(payload), headers=header_data)
print("Succesfully posted.")
And this works, I get an embed to post to Discord. The issue I am having is that another program generates a file, combined.wav, and I would like to send it to the same webhook. I have settled on splitting the File-sending process and the Embed process for ease of debugging, but the issue is that the code that sends the file sends it, but it has no data.
Here's the code I use to send the file:
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append('--' + boundary)
dataList.append('Content-Disposition: form-data; name=file; filename={0}'.format('C:\Users\Brigan\Desktop\EAS-APPS\Encoder\combined.wav'))
fileType = mimetypes.guess_type('C:\Users\Brigan\Desktop\EAS-APPS\Encoder\combined.wav')[0] or 'application/octet-stream'
dataList.append('Content-Type: {}'.format(fileType))
dataList.append('')
with open('C:\Users\Brigan\Desktop\EAS-APPS\Encoder\combined.wav') as f:
dataList.append(f.read())
dataList.append('--'+boundary+'--')
dataList.append('')
body = '\r\n'.join(dataList)
payload = body
headers = {
'Content-Type': 'multipart/form-data; boundary=--------------------------248330258300818905783969',
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/api/webhooks/688944637209083926/vAv6UScav_j1AUun8GZ1RnYlYd2JvcvGvR1Wv5Db7_av3NhrBTsLWLlxH5PT8b0yQlpy", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Does anyone know how to send a .WAV file to Discord in Python, or am I just outta luck?
**EDIT
I found out that the system named discord-webhooks is cross platform, so I jsut used that using the following code:
#Post Json Embed to Discord!
webhook = DiscordWebhook(url='webhook1')
embed = DiscordEmbed(title=lines, description="Issued "+datetime.now().strftime('%m/%d/%Y %H:%M:%S') + " " + TMZ, color=0xff9900)
embed.set_author(name='EAS Software', icon_url='https://www.fema.gov/sites/default/files/styles/unicorn_rotator/public/uni-rotator-imgs/eas_logo_rev1_3.png?itok=56iK9r2a')
embed.set_footer(text='NERP EAS Logger System')
embed.set_timestamp()
embed.add_embed_field(name='EAS Text Data:', value=args.text, inline=False)
embed.add_embed_field(name='EAS Protocol Data:', value="ZCZC-"+args.org+"-"+args.event+"-"+args.fips+"+"+args.time+"-"+ts_val+"-"+args.calls+"-", inline=False)
webhook.add_embed(embed)
response = webhook.execute()
webhook = DiscordWebhook(url='webhook1')
with open("Output/FULL-ALERT-DATA.wav", "rb") as f:
webhook.add_file(file=f.read(), filename='Alert-Audio.wav')
response = webhook.execute()
print("Succesfully posted.")
Thanks for the help!
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