This repository contains two Python scripts that demonstrate how to send emails with attachments using the Infobip Email API. The scripts utilize two different libraries:
requests
(for synchronous HTTP requests)aiohttp
(for asynchronous HTTP requests)
- Send emails with custom HTML content.
- Attach a file (e.g., a PDF receipt) to the email.
- Specify the sender's name and email address.
- Choose between synchronous (
requests
) and asynchronous (aiohttp
) implementations.
This script uses the requests
library to send an email with an attachment.
- Synchronous Implementation: Each API call blocks the program until it completes.
- Simple to set up and execute.
- Suitable for straightforward tasks without concurrency.
This script uses the aiohttp
library for an asynchronous approach to sending emails with attachments.
- Asynchronous Implementation: Supports non-blocking I/O operations, making it efficient for tasks requiring high concurrency.
- Requires Python's
asyncio
to manage event loops and tasks.
-
Infobip Account: Ensure you have an active Infobip account with access to the Email API.
-
API Key: Obtain your Infobip API key from the Infobip Dashboard.
-
Python Environment: Install the required libraries:
requests
(for the synchronous script)aiohttp
(for the asynchronous script)
Use pip to install them:
pip install requests aiohttp
-
Email Setup:
- Replace
YOUR_API_KEY
,YOUR_EMAIL_ADDRESS
, andYOUR_EMAIL_FROM
in both scripts with your actual Infobip API key, sender email address, and sender name.
- Replace
-
File to Attach: Ensure the file you wish to attach (
receipt.pdf
in this case) is located in thefiles/
directory.
-
Modify the script to add your Infobip API details:
API_KEY = "YOUR_API_KEY" EMAILS_FROM_EMAIL = "YOUR_EMAIL_ADDRESS" EMAILS_FROM_NAME = "YOUR_EMAIL_FROM"
-
Execute the script:
python email_sender_requests.py
-
Check the console for the API response.
-
Modify the script to add your Infobip API details:
API_KEY = "YOUR_API_KEY" EMAILS_FROM_EMAIL = "YOUR_EMAIL_ADDRESS" EMAILS_FROM_NAME = "YOUR_EMAIL_FROM"
-
Execute the script:
python email_sender_aiohttp.py
-
Check the console for the API response.
The scripts send the following email body as an example:
<html>
<body>
<h1 style="color: #754ffe;">Dear Recipient,</h1>
<p>This is a <b>test message</b> from <i>Infobip</i>.</p>
<p>Have a <span style="color: green;">great day!</span></p>
</body>
</html>
- Security: Never hard-code sensitive information such as API keys in your scripts. Use environment variables or a configuration file for secure storage.
- Error Handling: Implement error handling for production use to capture and log API failures.
- Attachments: Ensure the file path and MIME type are correct for the attachment.
This repository is released under the MIT License. See the LICENSE file for details.
For issues or feature requests, please open an issue in this repository. For Infobip-related support, refer to the Infobip Help Center.