I have an APP on Azure that if I run locally from my laptop using CMD runs perfectly and will use the send email function. I can also run an email test using BASH and the email is sent instantly. Now the issue if I use the desktop/home screen version of the APP everything works except the send email function. Relevant Parts of Code (Python)
# Load environment variables
load_dotenv()
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
- def send_email(to_email, subject, content):
- message = Mail(
- from_email='App@My Domain.com', # Use your verified email address
- to_emails=to_email,
- subject=subject,
- plain_text_content=content
- )
- try:
- sg = SendGridAPIClient(SENDGRID_API_KEY)
- response = sg.send(message)
- print('Status Code:', response.status_code)
- print('Body:', response.body.decode('utf-8') if response.body else '') # Ensure body is in readable format
- print('Headers:', response.headers)
- except Exception as e:
- if hasattr(e, 'body'):
- print('Error Body:', e.body.decode('utf-8')) # Ensure body is in readable format
- else:
- print('Error:', str(e))
From App Rout:
print(f"Sending email to xyz@MY Domain.com with subject: {email_subject}")
print(f"Email content: {email_content}")
try:
send_email('xyz@My Domain.com', email_subject, email_content)
flash('Project submitted for quote successfully.', 'success')
except Exception as e:
flash(f'Project submitted for quote successfully, but email notification failed: {e}', 'warning')
return redirect(url_for('welcome'))
Test that runs from BASH:#!/bin/bash
#!/bin/bash
API_KEY="Send Grid Key"
EMAIL_TO="xyz@My Domain.com"
EMAIL_FROM="app@my domain.com"
SUBJECT="Test Email from Azure"
CONTENT="This is a test email from your Azure app."
curl --request POST
--url https://api.sendgrid.com/v3/mail/send
--header "Authorization: Bearer $API_KEY"
--header "Content-Type: application/json"
--data '{
"personalizations": [
{
"to": [
{
"email": "'"$EMAIL_TO"'"
}
],
"subject": "'"$SUBJECT"'"
}
],
"from": {
"email": "'"$EMAIL_FROM"'"
},
"content": [
{
"type": "text/plain",
"value": "'"$CONTENT"'"
}
]
}'
I am at a loose as to why the app won’t send an email from the Azures default domain. Any help is greatly appreciated.
Hoping to get my App running correctly.
Brent Brumfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.