|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Nylas SDK Example: Send Email with Special Characters |
| 4 | +
|
| 5 | +This example demonstrates how to send an email with special characters |
| 6 | +(accented letters) in the subject line using the Nylas Python SDK. |
| 7 | +
|
| 8 | +The example sends an email with the subject "De l'idée à la post-prod, sans friction" |
| 9 | +to demonstrate proper handling of UTF-8 characters in email subjects. |
| 10 | +
|
| 11 | +Required Environment Variables: |
| 12 | + NYLAS_API_KEY: Your Nylas API key |
| 13 | + NYLAS_GRANT_ID: Your Nylas grant ID |
| 14 | + RECIPIENT_EMAIL: Email address to send the message to |
| 15 | +
|
| 16 | +Usage: |
| 17 | + First, install the SDK in development mode: |
| 18 | + cd /path/to/nylas-python |
| 19 | + pip install -e . |
| 20 | +
|
| 21 | + Then set environment variables and run: |
| 22 | + export NYLAS_API_KEY="your_api_key" |
| 23 | + export NYLAS_GRANT_ID="your_grant_id" |
| 24 | + export RECIPIENT_EMAIL="recipient@example.com" |
| 25 | + python examples/send_email_demo/send_email_example.py |
| 26 | +""" |
| 27 | + |
| 28 | +import os |
| 29 | +import sys |
| 30 | +from nylas import Client |
| 31 | + |
| 32 | + |
| 33 | +def get_env_or_exit(var_name: str) -> str: |
| 34 | + """Get an environment variable or exit if not found.""" |
| 35 | + value = os.getenv(var_name) |
| 36 | + if not value: |
| 37 | + print(f"Error: {var_name} environment variable is required") |
| 38 | + sys.exit(1) |
| 39 | + return value |
| 40 | + |
| 41 | + |
| 42 | +def send_email(client: Client, grant_id: str, recipient: str) -> None: |
| 43 | + """Send an email with special characters in the subject.""" |
| 44 | + # Subject with special characters (accented letters) |
| 45 | + subject = "De l'idée à la post-prod, sans friction" |
| 46 | + |
| 47 | + body = """ |
| 48 | + <html> |
| 49 | + <body> |
| 50 | + <h1>Bonjour!</h1> |
| 51 | + <p>Ce message démontre l'envoi d'un email avec des caractères spéciaux dans le sujet.</p> |
| 52 | + <p>Le sujet de cet email est: <strong>De l'idée à la post-prod, sans friction</strong></p> |
| 53 | + <p>Les caractères accentués sont correctement préservés grâce à l'encodage UTF-8.</p> |
| 54 | + </body> |
| 55 | + </html> |
| 56 | + """ |
| 57 | + |
| 58 | + print(f"Sending email...") |
| 59 | + print(f" To: {recipient}") |
| 60 | + print(f" Subject: {subject}") |
| 61 | + |
| 62 | + try: |
| 63 | + response = client.messages.send( |
| 64 | + identifier=grant_id, |
| 65 | + request_body={ |
| 66 | + "subject": subject, |
| 67 | + "to": [{"email": recipient}], |
| 68 | + "body": body, |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + print(f"\n✓ Email sent successfully!") |
| 73 | + print(f" Message ID: {response.data.id}") |
| 74 | + print(f" Subject: {response.data.subject}") |
| 75 | + print(f"\n✅ Special characters in subject are correctly preserved") |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + print(f"\n❌ Error sending email: {e}") |
| 79 | + sys.exit(1) |
| 80 | + |
| 81 | + |
| 82 | +def main(): |
| 83 | + """Main function.""" |
| 84 | + # Get required environment variables |
| 85 | + api_key = get_env_or_exit("NYLAS_API_KEY") |
| 86 | + grant_id = get_env_or_exit("NYLAS_GRANT_ID") |
| 87 | + recipient = get_env_or_exit("RECIPIENT_EMAIL") |
| 88 | + |
| 89 | + # Initialize Nylas client |
| 90 | + client = Client(api_key=api_key) |
| 91 | + |
| 92 | + print("=" * 60) |
| 93 | + print(" Nylas SDK: Send Email with Special Characters Example") |
| 94 | + print("=" * 60) |
| 95 | + print() |
| 96 | + print("This example sends an email with the subject:") |
| 97 | + print(' "De l\'idée à la post-prod, sans friction"') |
| 98 | + print() |
| 99 | + print(f"Grant ID: {grant_id}") |
| 100 | + print(f"Recipient: {recipient}") |
| 101 | + print() |
| 102 | + |
| 103 | + # Send the email |
| 104 | + send_email(client, grant_id, recipient) |
| 105 | + |
| 106 | + print("\n" + "=" * 60) |
| 107 | + print("Example completed successfully! ✅") |
| 108 | + print("=" * 60) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
| 113 | + |
0 commit comments