Laravel 5.5 On demand notification with slack - laravel-5.5

According to this page, we can send a notification on demand without going through a model. How do I do the same using slack?
I want to do something like this:
$emailAddress = 'my email address';
Notification::route('mail', $emailAddress)
->route('slack', 'what do I put here?')
->notify(new TestNotification());
The code works without the slack line.
Edit: this StackOverflow question uses the on demand method

You should you put the Slack Webhook URL. See: https://laravel.com/docs/5.5/notifications#routing-slack-notifications
Notification::route('slack', 'https://hooks.slack.com/services/ZZZZ/YYYY/XXX')->notify(new DailyStats());

Related

Parsing XML returned by SendNotification in exchangelib

I'm working on a python project to process emails as they arrive in a Microsoft inbox. Specifically, I want to set up a push subscription, receive notifications when a new email hits the inbox, and then construct and send another email based on the body of the one just received.
I'm at a loss to understand where "response.data" comes from since I can't find a reference to it anywhere in the documentation. The comment says:
When the server sends a push notification, the POST data contains a 'SendNotification' XML document. You can use exchangelib in the callback URL implementation to parse this data:
And the code in the example is:
ws = SendNotification(protocol=a.protocol)
for notification in ws.parse(response.data):
# ws.parse() returns Notification objects
pass
Where might I find a reference to this response.data?
Thanks in advance.
response.data is just a placeholder for whatever your web server framework calls the request object. The above code snippet is meant to be used in the web server code that handles the push events that Exchange sends to the URL that you defined when you created the push notification.

How to parse multipart/form-data forwarded by Mailgun API on AWS Lambda?

Here is what I am trying to achieve: Users will be able to email me at test#myDomain.com which is integrated with the Mailgun service. Based off a rule I'll have set Mailgun will forward this email as a POST request to https:url-to-my-lambda-function.amazonaws.com/email. I then receive this POST request in Lambda, parse it, then grab and use the html from the email for further work.
The issue I am running into is that Mailgun sends their POSTs not as JSON but as a multipart/form-data. I have not been able to find a way to parse this content type and grab the email html. Here is a sample forwarded email from Mailgun (note that technically mailgun sends along attachments too, but all I care about is the stripped-html field).
Also note that I am working with the Serverless framework and need to write this function in either Python or Node.js.
Things I have tried to do to parse the POST request:
Tried using the parse-multipart npm library, but was unable to get it to work I think because I couldn't find the boundary string. This might still be a tenable solution.
Tried let body = new String(decodeURIComponent(event.body)); , to put the data into a string and decode it because it seemed to be coming in URL encoded. Then I was using regex to find and pull out the stripped-html that I wanted. However, I think the URL decoding wasn't properly working because it was converting spaces into pluses. See the block below as an example of what I got:
<td+align="center"+valign="top"+id="m_-6910385412628668961m_-3845437051063103019m_-3682438291175620773bodyCell"+style="padding-bottom:40px;height:100%;margin:0;padding:0;width:100%;border-top:0">\n++++++++++++++++++++++++\n++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+style="border-collapse:collapse">\n++++++++++++++++++++++++++++<tbody><tr>\n++++++++++++++++++++++++++++++++<td+align="center"+valign="top">\n++++++++++++++++++++++++++++++++++++\n++++++++++++++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+id="m_-6910385412628668961m_-3845437051063103019m_-3682438291175620773templatePreheader"+style="border-collapse:collapse;background-color:#eaeaea;border-top:0;border-bottom:1px+solid+#d0d0d0">\n++++++++++++++++++++++++++++++++++++++++<tbody><tr>\n++++++++++++++++++++++++++++++++++++++++\t<td+align="center"+valign="top">\n++++++++++++++++++++++++++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="600"+style="border-collapse:collapse">
I have also tried using libraries like busboy to decode the multipart/form-data but have had issues with getting them working in Lambda because they work off of requests.
How can I parse this POST request? Is this even possible?
You should probably consider using a module designed to parse multipart form data such as https://www.npmjs.com/package/parse-formdata.

How to setup the "List-Unsubscribe" header in Amazon AWS SES API using the sendEmail function in PHP?

I'm trying to setup the "List-Unsubscribe" header in Amazon AWS SES API using the sendEmail function in PHP. I've reviewed Amazon's documentation and many other posts here but haven't found anything that works yet. This is what I'm currently using:
$client->sendEmail(array(
// all the main stuff like 'source', 'message', 'body', etc
'List-Unsubscribe' => '<http://unsubscribe.link>',
));
... where http://unsubscribe.link is my unsubscribe URL.
Any suggestions?
You'll need to use sendRawEmail instead of sendEmail in order to construct the raw message and add the List-Unsubscribe header to it.
TIP: It's better to use mailto-links instead of http-links as in List-Unsubscribe: <mailto:unsubscribe#example.com> since not all mail clients support http unsubscribe links.
Since late 2019 there is "SES v2", so the the method name is sendEmail, but the advice is essentially the same. There is a rawEmail option within sendEmail that you need to use to be able to set your own headers.

Getting the original sender when forwarding emails

How can I get the address of the original sender, when an email has been forwarded to Mailgun?
The chain of events looks like this:
originalSender sends message to someUser
someUser forwards message to Mailgun
Mailgun POSTs a parsed message to my server
Put in another way:
orignalSender (send)-> someUser (forward)-> mailgun (POST)-> myserver
The best I could get is doing a regex on the "body-plain" property.
The problem is that email clients do send this differently. Here are two examples.
Forwarding from GMail (I added the ...):
body-plain: "---------- Forwarded message ----------\r\nFrom: Kalle Kalleson <kalle.kalleson#mail.com>\r\nDate: 2014-02-13\r\n ..."
Forwarding from Apple's Mail (I added the ...):
body-plain: "(...)Begin forwarded message:\r\n\r\n> From: Kalle Kalleson <kalle.kalleson#mail.com>\r\n> Subject: New color printer\r\n> Date: 11 February, 2014 15:47:19 GMT+1\r\n>
There must be a better way of doing this, right?
Thanks in advance!
I've just been in contact with Mailgun support and they could not offer a different strategy.
That is, parsing the body myself, taking in account the differences between email clients.
Lame I would say, :-(
Here you can vote up the feature request.
http://mailgun.uservoice.com/forums/156243-general/suggestions/5528656-extract-the-original-sender-of-a-forwarded-email
Has anyone come up with a better answer?
Perhaps I am missing what you are looking for, but when Mailgun POSTs to your server, you should be able to pull the From field from the POST data. I'm using a node.js app to parse my messages, however, in PHP it would look something like:
<?php
$from = $_POST["From"];
echo "This message is from: ".$from;
?>
I apologize if I'm missing what you're asking.
Using a regular expression should do the trick in either case.
Try:
/(From:.*>)/g
Not possible to take original sender of an email throw any mail services.
So, we implemented regex and take the first occurence of the match from mail html body.
Regex.Match only returns first match so used this with below regex.
From:\s(.*?)>
https://regex101.com/r/1pUpPU/1

How to pipe an email into Django?

I'm part of a two-man business selling LED glow toys and one aspect of this is handling support requests. I have a server running exim4 and DJango and have email working so that if a user sends an email to support#myhost.com I'm able to pick up the email and respond.
I'd like to develop something a bit tidier to keep track of the email chain for a particular support request.
To do this, I was thinking of piping the output of my email using a rule in my support email's filter:
pipe /usr/bin/email_to_django_script
What I'm unsure of is how best to go about the last step to actually turning the email content into something DJango can process.
What's the best method to do this? Would a script using curl -d be a sensible option or are there better / less convoluted ways?
You can pipe the output of the email server into a management command. As an example, I have a file /inquiries/management/commands/proc_email.py. I have a single Command class, and the handle() method gets most of the email from the environment, and the body of the email from STDIN:
from_email = strip_tags(os.environ.get('SENDER', None))
to_email = strip_tags(os.environ.get('RECIPIENT', None))
emailMessage = email.message_from_string(''.join(sys.stdin.readlines()))
There is other code in there, but that is how I get the important bits out of it. You can then pipe this into your ORM objects, and access it from the website at some later time.
This is then accessed through /path/to/project/manage.py proc_email.
Depending on your email server, you can also use plus addressing to insure replies come back to the same address. For example, I have my Reply-To headers set to inquiry+12345#whatever.com. The mail server (postfix) then dumps this into the environment under EXTENSION. If no number is supplied, I simply create a new Inquiry, instead of attaching to an existing one.
Not exactly a pure Django solution but I would recommend taking a look at Lamson Project. It's an email server written in Python that you can use to build email applications like what you are describing. I can also integrate with the Django ORM. http://lamsonproject.org/docs/hooking_into_django.html