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?
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")
Related
I am attempting to make an HTTP PUT request from XLRelease to update data in Adobe Workfront. I have been able to successfully login using the API client and GET data. I have also been able to successfully update data using Postman as well as using a native Python script. I am using the HttpRequest library within XLR. I am receiving the same response back in XLR as I am when successfully updating when using Postman, however, the data is not updated when using XLR.
My code is as follows:
import json
WORKFRONT_API_HOST = releaseVariables['url']
WORKFRONT_API_VERSION = releaseVariables['wfApiVersion']
WORKFRONT_API_KEY = releaseVariables['apiKey']
WORKFRONT_USERNAME = releaseVariables['wfUsername']
FI_ID = releaseVariables['target_customer_id']
newPortfolioId = releaseVariables['target_portfolio_id']
WORKFRONT_API_URL = WORKFRONT_API_HOST + WORKFRONT_API_VERSION
def wfLogin():
sessionID = ""
login_endpoint = "/login"
login_request = HttpRequest({'url': WORKFRONT_API_URL})
login_response = login_request.get(login_endpoint + "?apiKey=" + str(WORKFRONT_API_KEY).replace("u'","'") + "&username=" + WORKFRONT_USERNAME, contentType='application/json')
if login_response.getStatus() != 200:
print('# Error logging into WF\n')
print(login_response.getStatus())
print(login_response.errorDump())
sys.exit(1)
else:
json_response = json.loads(login_response.getResponse())
print ("Logged in to WF")
sessionID = json_response['data']['sessionID']
return sessionID
def wfLogout(sessionID):
logout_endpoint = "/logout"
logout_request = HttpRequest({'url': WORKFRONT_API_URL})
logout_response = logout_request.get(logout_endpoint + "?sessionID=" + sessionID, contentType='application/json')
if logout_response.getStatus() != 200:
print('# Error logging out of WF\n')
print(logout_response.getStatus())
print(logout_response.errorDump())
sys.exit(1)
else:
json_response = json.loads(logout_response.getResponse())
print ("Logged out of WF")
result = []
session_id = wfLogin()
if session_id != "":
customer_request = HttpRequest({'url': WORKFRONT_API_URL})
endpoint = '/prgm/%s?sessionID=%s&portfolioID=%s&customerID=%s' % (FI_ID, session_id, newPortfolioId, FI_ID)
jsonObj = "{}"
payload = {}
customer_response = customer_request.put(endpoint, jsonObj, contentType='application/json')
if customer_response.getStatus() != 200:
print('# Error connecting to WF\n')
print(customer_response)
print(customer_response.getStatus())
print(customer_response.errorDump())
sys.exit(1)
else:
response_json = json.loads(customer_response.getResponse())
print ("response_json: ", response_json)
#log out of current session
wfLogout(session_id)
else:
print ("No sessionID is available")
sys.exit(1)
How would you send an email using Lua?
The team I'm working with have a mail server, is that of any relevance?
Here is the code I'm using:
function send_email (email_to, email_subject, email_message)
local SMTP_SERVER = "mail.server.com"
local SMTP_AUTH_USER = "mail#domain.com"
local SMTP_AUTH_PW = "password"
local SMTP_PORT = "587"
local USER_SENDING = "mail#domain.com"
local smtp = require("socket.smtp")
local rcpt = {email_to}
local mesgt = {
headers = {
to = email_to,
from = USER_SENDING,
subject = email_subject
},
body = email_message
}
local r, e = smtp.send{
from = USER_SENDING,
rcpt = rcpt,
source = smtp.message(mesgt),
server = SMTP_SERVER,
port = SMTP_PORT,
user = SMTP_AUTH_USER,
password = SMTP_AUTH_PW
}
end
Using the LuaSocket SMTP API.
Your example looks correct, double check the SMTP settings and log the results:
local r, e = smtp.send{
from = USER_SENDING,
rcpt = rcpt,
source = smtp.message(mesgt),
server = SMTP_SERVER,
port = SMTP_PORT,
user = SMTP_AUTH_USER,
password = SMTP_AUTH_PW
}
-- Log SMTP results and potential errors
print(r, e)
Also, ensure that you're properly chaining your SMTP message using the LTN12 module API when it is multipart:
body = ltn12.source.chain(
ltn12.source.file(io.open("image.png", "rb")),
ltn12.filter.chain(
mime.encode("base64"),
mime.wrap()
)
)
Or the Mime module API for the EOL:
body = mime.eol(0, [[
Lines in a message body should always end with CRLF.
The smtp module will *NOT* perform translation. However, the
send function *DOES* perform SMTP stuffing, whereas the message
function does *NOT*.
]])
There is a much more verbose example of this in the LuaSocket SMTP API documentation.
I'm writing a script to send an email to more than one email account, but not able, yet.
It works as it is below, but if I set receivers='xxx#xxx.com','yyy#yyy.com' it won't work, it throws an error:
AttributeError: 'tuple' object has no attribute 'encode'.
How can I set receivers=?
def send_email (out_file):
sender = 'xxx#xxx.com'
receivers = 'xxx#xxx.com'
email_pass = 'aaaa'
filematch=re.findall('NE.*\.txt',out_file.name)
subject = ("NEXXXX_price_update")
message = ("The following file was forwarded to your ftp account %s " %filematch)
msg = 'Subject: %s\n%s' %(subject, message)
try:
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com',0)
smtpObj.login(receivers, email_pass)
smtpObj.sendmail(sender, receivers, msg)
print ("Successfully sent email")
except SMTPException:
print ("email NOT successful")
print(SMTPException.__cause__)
smtpObj.quit()
You assign wrongly
receivers='xxx#xxx.com','yyy#yyy.com'
You suppose to assign as a tuple or list, not sure 100% which.
Give a try:
receivers=('xxx#xxx.com','yyy#yyy.com')
or
receivers=['xxx#xxx.com','yyy#yyy.com']
This has me totally stumped. Here are two simple routines that send an email. The first succeeds, the second (with some minor exception trapping) locks up Python (Cntl-C will not kill the session). In addition I've determined that the quote type used for the "Subject" line matters. So if I use
msg['Subject'] = "Random text"
The email will fail, but
msg['Subject'] = 'Random text'
this succeeds
Can someone please shine some light into the darkness:
Example #1
import smtplib
from email.mime.text import MIMEText
addr = "10.0.0.178"
From = "me#somewhere.net"
recip = ["me#somewherelese.net","foo#bar.net"]
msg = "Message Body"
send_email(recip,"Subject Line",msg)
def send_email(To,Subj,Mess):
msg = MIMEText(Mess)
msg["Subject"] = Subj
msg['From'] = From
s = smtplib.SMTP(addr)
for entry in To:
msg['To'] = entry
s.sendmail(From, [entry], msg.as_string())
s.quit()
Example 2:
import smtplib
from email.mime.text import MIMEText
addr = "10.0.0.178"
From = "me#somewhere.net"
recip = ["me#somewherelese.net","foo#bar.net"]
msg = "Message Body"
send_email(recip,"Subject Line",msg)
def send_email(To,Subj,Mess):
msg = MIMEText(Mess)
msg["Subject"] = Subj
msg['From'] = From
s = smtplib.SMTP(addr)
for entry in To:
msg['To'] = entry
try:
s.sendmail(From, [entry], msg.as_string())
except:
print "Error sending email"
s.quit()
I have the following script, which I found online, in Python. What it does is tries to connect to a MineCraft server, first by sending a 'handshake', then sending a login request. The protocol specs can be found here: http://wiki.vg/Protocol
Anyway, the python script works fine. However, I think the second packet is encoded wrong, as when it is sent, nothing appears on the server console. The player isn't connected or anything. It just eventually times out and closes the connection due to the 'client' not logging in in time.
Basically, anyway who has experience with struct.pack() should be able to help me here. I have commented the line where I am unsure of whether I have encoded everything right. The detailed information on packing the data is shown in the link above.
Any help would be greatly appreciated, I'm pretty clueless with encoding/packing data. :(
Here's the code
import struct
import socket
import time
import urllib
import urllib2
host = str(raw_input('What is the host ip: '))
port = int(raw_input('What is the server port: '))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
usrnm = str(raw_input('What is your username: '))
psswrd = str(raw_input('What is your password: '))
logindata = {'user':usrnm, 'password':psswrd, 'version':'12'}
data = urllib.urlencode(logindata)
print('Sending data to login.minecraft.net...')
req = urllib2.Request('https://login.minecraft.net', data)
response = urllib2.urlopen(req)
returndata = response.read()
returndata = returndata.split(":")
mcsessionid = returndata[3]
del req
del returndata
print("Session ID: " + mcsessionid)
data = {'user':usrnm,'host':host,'port':port}
enc_user = data['user'].encode('utf-16BE')
packfmt = '>bih{}shiibBB'.format(len(enc_user))
packetbytes = struct.pack(packfmt, 1, 23, len(data['user']), enc_user, 0, 0, 0, 0, 0, 0)
stringfmt = u'%(user)s;%(host)s:%(port)d'
string = stringfmt % data
structfmt = '>bh'
packetbytes = struct.pack(structfmt, 2, len(string))+string.encode('utf-16BE')
s.send(packetbytes)
connhash = s.recv(1024)
print("Connection Hash: " + connhash)
print('Sending data to http://session.minecraft.net/game/joinserver.jsp?user=' + usrnm + '&sessionId=' + mcsessionid + '&serverId=' + connhash + '...')
req = urllib.urlopen('http://session.minecraft.net/game/joinserver.jsp?user=' + usrnm + '&sessionId=' + mcsessionid + '&serverId=' + connhash)
returndata = req.read()
if(returndata == 'OK'):
print('session.minecraft.net says everything is okay, proceeding to send data to server.')
else:
print('Oops, something went wrong.')
time.sleep(5)
# All above here works perfectly.
enc_user = data['user'].encode('utf-16BE')
packfmt = '>bih{}shiibBB'.format(len(enc_user))
packetbytes = struct.pack(packfmt, 1, 23, len(data['user']), enc_user, 0, 0, 0, 0, 0, 0)
#This line is probably where something's going wrong:
packetbytes = struct.pack('>bih', 1, 23, len(data['user'])) + data['user'].encode('utf-16BE') + struct.pack('>hiibBB', 2,0,0,0,0,0)
print(len(packetbytes))
print('Sending ' + packetbytes + ' to server.')
s.send(packetbytes)
while True:
data = s.recv(1024)
if data:
print(data)
Yeah, you're sending the length of the string, which is the number of characters. Instead, you should be sending the number of bytes in the encoded string. Also, you should use "!" instead of ">" for clarity's sake, as "!" is used to indicate "network order", which this is. So this...
structfmt = '>bh'
packetbytes = struct.pack(structfmt, 2, len(string))+string.encode('utf-16BE')
... gets changed to this...
structfmt = '!bh'
encoded = string.encode('utf-16BE')
packetbytes = struct.pack(structfmt, 2, len(encoded))+encoded