This script demonstrates how to send emails with attachments using the Infobip Email API and Python's requests
library.
- 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.
- Utilize Infobip's API for robust email delivery.
-
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: The script is written in Python and requires the following modules:
requests
mimetypes
email.utils
You can install the
requests
library using pip:pip install requests
-
Email Setup:
- Replace
YOUR_API_KEY
,YOUR_EMAIL_ADDRESS
, andYOUR_EMAIL_FROM
in the script 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.
-
Clone or Download the Script Clone the repository or copy the script into your project directory.
-
Modify Configuration Update the following variables in the script:
API_KEY = "YOUR_API_KEY" EMAILS_FROM_EMAIL = "YOUR_EMAIL_ADDRESS" EMAILS_FROM_NAME = "YOUR_EMAIL_FROM"
-
Run the Script Execute the script:
python email_sender.py
-
Check the Response The script prints the API response to the console. Verify that the email was sent successfully.
-
Form Data: The
data
dictionary contains fields such asfrom
,to
,subject
, andhtml
for email content. -
File Attachment: The
files
dictionary includes the file to be attached. MIME type is determined automatically using themimetypes
module. -
Request: A POST request is sent to the Infobip Email API with the form data, headers, and attachment.
The script sends 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 script 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.