How do i send email from Azure function app

对着背影说爱祢 提交于 2020-05-29 10:49:24

问题


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?


回答1:


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/




回答2:


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



来源:https://stackoverflow.com/questions/58246398/how-do-i-send-email-from-azure-function-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!