How Sending emails work with Azure logic apps? - azure

when we design one logic app to send mail to required TO address list from the given mail sender(gmail, outlook), how actually the email sending works in azure logic app?
Do we need any Prerequisites like SMTP and relay service required here?
Or everything will be handled by Microsoft using logic app?

You need an SMTP server and credentials to it.
You can send emails using the connector - "SMTP" with action "Send Email (V3)"
The tricky part would be to setup the API connection for this action. Once, you have the server details on hand, you can "Add New" connection in the below screen:

Related

Send azure monitor alert to slack not working

I am sure this has been asked many times but am unable to find relevant answer. Is there any way to send Azure monitor alerts to slack via Logic apps or some other way. I have found this template - https://github.com/Azure/azure-quickstart-templates/tree/master/demos/alert-to-slack-with-logic-app and integrated it into my azure system but it is not working.
Have tried some work around mentioned at - https://github.com/Azure/azure-quickstart-templates/issues/3319 but all in vain
Direct Webhook from action group using slack incoming webhook is not supported , as azure sends alert using its own schema.
Any input is highly appreciated ...
Azure Logic Apps is definitely an option, but if you are not needing complicated workflows, just send the alert directly from Azure Monitor to a Slack channel. You can do this by generating a unique email for the Slack channel and using that email in your Azure Monitor alert action group settings. Another option would be to send the alert to your company alert email inbox and have it automatically forwarded to that Slack channel email address. With this approach you completely remove the dependency and complexity of Azure Logic Apps.
https://slack.com/help/articles/206819278-Send-emails-to-Slack#h_01F4WDZG8RTCTNAMR4KJ7D419V

How do I send email from an Azure function app?

I have running Azure function App(in python language), for business requirements need to send emails from function app.
for that, I wrote a python function
from email.message import EmailMessage
import smtplib
def send_mail():
# message to be sent
msg = EmailMessage()
msg.set_content('Test content')
msg['Subject'] = 'Test'
msg['From'] = "test#hotmail.com"
msg['To'] = ["test#hotmail.com"]
# creates SMTP session
s = smtplib.SMTP('smtp-mail.outlook.com', 587)
s.ehlo()
# start TLS for security
s.starttls()
# Authentication
s.login("test#hotmail.com", "password-to-login")
# sending the mail
s.send_message(msg)
# terminating the session
s.quit()
return
Above block of code working fine in local machine. if I move the same code to Azure Function App it's not working.
How do I make it work on the Azure function app?
How do I send email from Gmail in Azure Function App?
Sending outbound e-mail to external domains (such as outlook.com, gmail.com, etc) directly from an e-mail server hosted in Azure compute services is not supported due to the elastic nature of public cloud service IPs and the potential for abuse.  As such, the Azure compute IP address blocks are added to public block lists (such as the Spamhaus PBL).  There are no exceptions to this policy.
Since we do not support running and smtp server from our platform this should not affect us.
The only way to use EMAIL functionality as of now on Azure Web App is via an SMTP relay. A third party service such as SendGrid provides these type of services.
In the Azure Web Apps architecture the actual Web Apps sit behind common Front-Ends which are shared by all the sites hosted on that Data Centre.
There is a possibility that one of the site hosted on that datacenter is sending SPAM emails and this could have the IP address to be blacklisted by the MAIL Servers. So the e-mails sent from that address will be rejected or considered as SPAM by mail servers.
 
This limitation exists in case of VM or Cloud Services too. Azure uses a pool of IP Address, and these addresses are reused. That means you could get an IP Address which has already been blacklisted, as someone was sending SPAM from that address before and hence your emails would be rejected or considered as SPAM by mail servers.
This is a common scenario in Cloud and it is typically recommended to use an external Mail Service provider like SendGrid for messaging.
 
SendGrid related articles: 
How to Send Email Using SendGrid with Azure: https://azure.microsoft.com/en-in/documentation/articles/sendgrid-dotnet-how-to-send-email/
I would suggest you to use some 3rd party email service such as SendGrid/Twillio
You can add SendGrid output binding to you Python Azure Function. The binding in function.json would look something like here
Here is an example
Below is the actual procedure for getting sendgrid working manually with python for azure functions. This is not the optimal way since azure has it's own in house solution that may be superior to this, but this is a quick and dirty way to get it working.
run pip install sendgrid on your local machine.
In your code include:
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
Sending The Email: (keep in mind this is NOT SECURE since the API Key is Exposed, but this is quick way to get something working.)
message = Mail(
from_email='yourfromemail#gmail.com',
to_emails='toemail#gmail.com',
subject='Email Subject Line Here',
html_content='<strong>HTML CONTENT HERE</strong>')
try:
sg = SendGridAPIClient('YOUR API KEY HERE')
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
raise Exception(str(e) + ' -> Error Reported')
The last step is if you want this to work on the actual azure function server you need to add this to requirements.txt
sendgrid
Note that sendgrid is free up to 25,000 emails so it works perfect for most situations. You just need to setup your API key on there website, just follow their instructions, it was simple for me to do and took about 5 minutes to create a 'user' and an API key associated with it.
Also note that I was able to send from my GMAIL account when I did this with sendgrid. HOWEVER, also note that it will go to your spam folder constantly because google can never truly verify it was you who sent from your gmail, so just keep this flaw in mind. In my case since I'm doing just testing work this doesn't matter, but it may be a problem for production work.
There is a hack that I'm about to implement. In my case I don't really care about the subject, attachments etc. It's an information email.
Add a log message in your code with email body in a parsable form.
Create a log based alert rule matching the log.
When Azure Monitor sees this log, it'll send an alert to the email address(es) configured in the alert. Email can include contents of log message.
As mentioned this is very restrictive:
can't fully control subject line
can't control the recipients programatically (easily at least)
can't attach files

How can I setup an Outlook notification webhook on Azure?

Currently we have subscription in Office 365. All users has access to Outlook emails. We are planning to build a batch job using Daemon or Server application (as described here https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api). The reason for the batch job to be as Daemon service is because we wanted to access all user emails, filter and forward to another mail server with one app identity.
Question 1 : How can we grant an app in Azure to access all users emails ?
As an alternative we could also build a webhook that is subscribed to inbound/outbound emails and once we get a notification in our system we can filter/forward to another mail server. https://dev.outlook.com/restapi/concepts/webhooks
Instead of building a job and calling a REST API, is there a way to
register a webhook in Azure for all users at once and redirect the
url to our system ?
That will save us from building a batch job that has to run everyday to grab all incoming emails and then filter. If we can integrate a webhook all we have to build is just the filter/forward part.
The Webhooks are only supported for user level credentials. i.e Each user has to give the app a permission to request data on behalf of users. However, in our case, we need to get notifications for all user emails to reach our application and this is impossible to do at the time of writing (Sep 2016).
The solution we went forward is to build a batch job to pull all data. An Admin has to give an access to the application (the application will have its own identity) and make subsequent requests. For the app to identify itself, we have used X.509 certificate
We followed this documentation and this
If you want to redirect the specific messages with Office 365, a easy way is config the mail flow on the Exchange Admin Center.
For example, we can config a rule to redirect to the messages to the mailbox you wanted as below:
Here is the document for the mail flow rules for your reference.

How can I use SMTP and Gmail in an end user application?

I am working on an application that requires communication via email with the user and the client for whom the application is being developed.
Basically the flow is thus :
Program Does Something
Program sends an email detailing if it worked or not to the user and my client
We have established a Gmail account to make this happen. Unfortunately my clients users are multinational and Gmail freaked out and started blocking users, which started throwing exceptions when they were trying to use the program.
In the emails I saw when I checked the account, I saw I see at least two of these emails that say "Suspicious sign in prevented", and there was a red bar that said multiple attempts were tried to sign in and asked if it was me, and there were points all over the world.
How would I go about making it so that Gmail doesn't freak out when it sees users trying to sign in all over the planet?
Isn't sending through a some kind of a server application not an option? I.e. instead of making SMTP connection from your end user app directly to Gmail server your app sends a request to your web server which does actual SMTP connection. This way you'll also eliminate issues when SMTP ports are blocked on end users' machines.

Sending email from ASP.NET application using SMTP server in IIS6; email gets sent without error but the mail gets stuck in mailroot\drop folder

I have a SMTP server set up for my domain in IIS6. The mail sends just fine from the site, there are no errors. But the email is never delivered to the recipient. I checked my mail folder on the server and the emails are stuck in \inetpub\mailroot\drop
Any idea why they wouldn't be getting sent out? This is new territory for me and I'm not having much luck finding a solution.
Since the mail is making its way into the mail folder, I assume the problem lies somewhere in my SMTP server settings
There could be a couple of reasons for this and it sounds to me that your provider has blocked port 25 which means your server can't send the emails, hence, they are still in the queue.
Your best bet is to use a 3rd party SMTP service such as Amazon SES or Mandrill (free), you can either configure your application to send mail directly through these providers, or, you can continue to use IIS SMTP and configure smart host, this will tell IIS SMTP to not bother trying to send email directly, but instead relay it through the 3rd party SMTP service.
More about SMTP and smart hosts here: http://support.microsoft.com/kb/303734/en-us

Resources