File size: 1,073 Bytes
8d7950f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import httpx
import asyncio
from app.core.config import settings

async def test_sendgrid():
    client = httpx.AsyncClient()
    headers = {
        'Authorization': f'Bearer {settings.SENDGRID_API_KEY}',
        'Content-Type': 'application/json'
    }
    payload = {
        'personalizations': [{'to': [{'email': 'alkassem.ashraf@gmail.com'}], 'subject': 'Diagnostic Integration Test'}],
        'from': {'email': settings.SENDGRID_FROM_EMAIL},
        'content': [{'type': 'text/plain', 'value': 'Verifying SendGrid API integration.'}]
    }
    
    print(f"API Key Starts With: {settings.SENDGRID_API_KEY[:7] if settings.SENDGRID_API_KEY else 'NONE'}")
    print(f"From Email: {settings.SENDGRID_FROM_EMAIL}")
    
    try:
        r = await client.post('https://api.sendgrid.com/v3/mail/send', headers=headers, json=payload)
        print('STATUS:', r.status_code)
        print('BODY:', r.text)
    except Exception as e:
        print('EXCEPTION:', repr(e))
    finally:
        await client.aclose()

if __name__ == '__main__':
    asyncio.run(test_sendgrid())