Email from Azure using SendGrid

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Load environment variables
load_dotenv()
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
</code>
<code># Load environment variables load_dotenv() SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY') </code>
# Load environment variables
load_dotenv()
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>- 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))
</code>
<code>- 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)) </code>
- 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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'))
</code>
<code>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')) </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/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"'"
}
]
}'
</code>
<code>#!/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"'" } ] }' </code>
#!/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.

New contributor

Brent Brumfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật