Google Mail API - parse get message headers - regex

Currently I am able to get a full message for a user, but I am trying to get to all the users that are on the "To" header of the message. I am able to get the header value, but hoped there would be an sdk that would actually parse this header for me, as there are a bunch of different formatting of the user info. I am using the node googleapis npm package. If there is not something build in, is there a regex that I could use to parse this "To" mail header?
const to = _.find(message.payload.headers,
(header) => header.name.toLowerCase() === 'to');
'to' now has the value of the header, but this value is just a string of users all formatted in possibly different ways. I need the user's name and email address of each user.
const users = [{name: <name>, email: <email> }]
Currently I am using a regex that is missing some possible formatting of this header.

Regexp may not be the best option for parsing an email To: field. I would recommend using something like https://github.com/andris9/mailparser instead.
Even if you don't have a raw copy of the email, it would be easy enough to reconstruct something similar by appending the strings for the headers and the payload.

Related

Chilkat Email Object - Save email without attachments

I need to save the email without the attachments in EML format with Chilkat.
I'm using VB.NET and an IMAP connection and I have already checked SaveAllAttachments (that does the opposite saving only the attachments) and SaveEml (for email with attachments included).
'Attachments only
email.SaveAllAttachments(MyInPath)
'Email with attachments in EML format
email.SaveEml(MyInPath & "\" & email.GenerateFilename())
I'd like to keep a regular EML format (with header, sender, subject, body erc.) but without the attachments (so not just the body on a TXT file).
Is there a way to do it simply?
Someone who deleted his post (I don't know why) said to use email.DropAttachments() and posted this link:
https://www.chilkatsoft.com/refdoc/vbnetEmailRef.html
It's a little bit strange to save the whole email and then delete the attachments but it simply works

How to get a readable email from AWS S3 after it's stored there as an object?

I've set up SES to receive emails on my domain and then store the emails to S3. I trigger an SNS notification when a new email has arrived which triggers a lambda to do processing with the contents inside the email. Everything works as expected however, I'm not able to get any sensible data out of the emails I fetch from S3. For instance, getting an object from S3 of the email gives me this data :
<div dir=3D"ltr">ssadsadasdasdas</div><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr" class=3D"gmail_attr">On Tue, Nov 5, 2019 at 5:30 PM Rahul Patil =
<<a href=3D"mailto:rahul.patil#name.com">rahul.patil#name.com<=
/a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0=
px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><=
div dir=3D"ltr">asdsadasdasdasd</div><br><div class=3D"gmail_quote"><div di=
r=3D"ltr" class=3D"gmail_attr">On Tue, Nov 5, 2019 at 5:27 PM <<a href=
=3D"mailto:rahul#name.com" target=3D"_blank">rahul#name.com</a>&g=
t; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0p=
x 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Body<b=
r>
The code which fetches the data
const obj = await s3.getObject(getObjectParams).promise();
console.log(obj);
let objectData = obj.Body.toString("utf-8");
console.log(objectData)
I don't need all that HTML, just the sender's email and the body would be sufficient. Is there an inbuilt way I can filter the required data? Any node-email-parser modules that can be plugged inside the lamba? More importantly, Am I doing it the right way? Thanks!
Yes, you need a parser.
Amazon SES will store incoming emails in S3 in RFC822 format, meaning exactly as they are received from the wire. This is by definition plain text, no matter how complex the email, even if it has attachments. Somewhere inside that RFC822 text piece there may or may not be some HTML in the body. An email's body can be plain text only, it can be HTML (most common) or it can be both.
You'll need to use a library which can parse RFC822. There are quite many of those. Which one to use will depend on your language choice. You'll also need to familiarize yourself with the anatomy of an Internet email message, i.e. RFC822. You'll find a wealth of information on that with a bit of googling. Suggestion: Your own email client can most likely save an email in RFC822 format and then you can use that as an example of what an email truly looks like in its 'native' format. Just have a look at it in your favorite text viewer.
Your question can be rephrased into an RFC822 parsing question. Some people refer to such files as .eml files. Same thing.
Happy hunting.

JMeter - How to extract values from a response which has been decoded from base64 and stored in a variable? All under the same sampler

I am trying to test a webservice's performance, and having a few issues with using and passing variables. There are multiple sequential requests, which depend on some data coming from a previous response. All requests need to be encoded to base64 and placed in a SOAP envelope namespace before sending it to the endpoint. It returns and encoded response which needs to be decoded to see the xml values which need to be used for the next request. What I have done so far is:
1) Beanshell preprocessor added to first sample to encode the payload which is called from a file.
2) Regex to pull the encoded response bit from whole response.
3) Beanshell post processor to decode the response and write to a file (just in case). I have stored the decoded response in a variable 'Output' and I know this works since it writes the response to file correctly.
4) After this, I have added 4 regex extractors and tried various things such as apply to different parts, check different fields, check JMeter variable etc. However, it doesn't seem to work.
This is what my tree is looking like.
JMeter Tree
I am storing the decoded response to 'Output' variable like this and it works since it's writing to file properly:
import org.apache.commons.io.FileUtils;
import org.apache.commons.codec.binary.Base64;
String Createresponse= vars.get("Createregex");
vars.put("response",new String(Base64.decodeBase64(Createresponse.getBytes("UTF-8"))));
Output = vars.get("response");
f = new FileOutputStream("filepath/Createresponse.txt");
p = new PrintStream(f);
this.interpreter.setOut(p);
print(Output);
f.close();
And this is how I using Regex after that, I have tried different options:
Regex settings
Unfortunately though, the regex is not picking up these values from 'Output' variable. I basically need them saved so i can use ${docID} in the payload file for next request.
Any help on this is appreciated! Also happy to provide more detail if needed.
EDIT:
I had a follow up question. I am trying to run this with multiple users. I have a field ${searchuser} in my payload xml file called in the pre-processor here.
The CSV Data set above it looks like this:
However, it is not picking up the values from CSV and substituting in the payload file. Any help is appreciated!
You have 2 problems with your Regular Expression Extractor configuration:
Apply to: needs to be response
Field to check: needs to be Body, Body as a Document is being used for binary file formants like PDF or Word.
By the way, you can do Base64 decoding and encoding using __base64Decode() and __base64Encode() functions available via JMeter Plugins. The plugins in their turn can be installed in one click using Plugin Manager

How to get email attachment MIME type by file extension?

I'm trying to write a class that would use Mandrill APIs to send an email with an attachment. To do that I need to provide MIME type of the attachment for the base-64 encoded attachment contents. The question is how to get it, assuming using the file extension of the attachment?
PS. I was hoping for something better than a long switch/case situation. But if that's my only option, where can I get the most exhaustive list of such associations?
You can look in the Registry, under either:
HKEY_CLASSES_ROOT\<file extension>\ and see if it has a "Content Type" value.
HKEY_CLASSES_ROOT\MIME\Database\Content Type\, enumerating each subkey until you find one whose "Extension" value contains the file extension.
There is also a FindMimeFromData() function.
If you don't find a matching content type, you can always use application/octet-stream.
Apart to what was suggested in a different answer, here's a hard-coded list of file-extension-to-MIME-types associations that I converted from this Ruby project's JSON list into a C-struct.
Oops, can't post it here. It's too long. Here's the file instead.

How will I filter out only errors in Jenkins-email-ext, BUILD_LOG_REGEX?

Currently I m using BUILD_LOG_REGEX in Jenkins Editable email information to get a log of the errors via email. But I get a lot of junk and I want to filter out the errors and I want the log of errors filtered to perfection. Any help?
Your question is rather non-specific. As Juuso Ohtonen notes in a comment, what you do highly depends on what can be usually found in your log. Here's an example of what we use in one of our jobs, it is rather generic (if not to say minimalistic):
${BUILD_LOG_REGEX, regex="^.*?BUILD FAILED.*?$", linesBefore=0, linesAfter=10, maxMatches=5, showTruncatedLines=false, escapeHtml=true}
I would suggest the following: create a job that logs some text that contains types of errors you encounter (you may just spew some text file that you place in the job's workspace), then play with Java regex patterns - java.util.regex.Pattern - in the Plugin until you get the desired result. Make sure you send the e-mails from the job only to yourself :)
To use custom HTML - here's a quote from the Plugin's Content Token reference:
${JELLY_SCRIPT, template} - Custom message content generated from a Jelly script
template. There are two templates provided: "html" and "text". Custom Jelly templates
should be placed in $JENKINS_HOME/email-templates. When using custom templates, the
template filename without ".jelly" should be used for the "template" argument.
template - the template name. Defaults to "html".
The default template that you can use as your starting point is located in
$JENKINS_HOME/plugins/email-ext/WEB-INF/classes/hudson/plugins/emailext/templates/html.jelly