Skip to content

GiapKun/send-mail-with-resend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Resend Email Service

A Python project that demonstrates how to send emails using the Resend API with templated HTML content. This project uses Jinja2 for rendering templates and Loguru for logging.

Features

  • Email Sending: Send emails using the Resend API.
  • Template Rendering: Use Jinja2 to create dynamic, data-driven email templates.
  • Logging: Monitor email sending with Loguru logging.

Prerequisites

  1. Python 3.7+
  2. A Resend API key (Sign up for Resend).

Installation

  1. Clone the repository:

    git clone https://github.com/GiapKun/send-mail-with-resend.git
    cd <send-mail-with-resend>
  2. Install the required dependencies:

    pip install resend jinja2 loguru
  3. Set up your email templates:

    • Create a templates folder in the project directory.
    • Add your email templates (e.g., welcome.html) to this folder.
  4. Configure your API key and sender information:

    • Replace placeholders in the Config class with your values:
      RESEND_API_KEY = "your-resend-api-key"  # Replace with your Resend API key
      SENDER_EMAIL = "[email protected]"  # Replace with your sender's email
      SENDER_NAME = "Your Sender Name"  # Replace with your sender name

Usage

1. Initialize the EmailService

email_service = EmailService(
    api_key=Config.RESEND_API_KEY,
    sender_email=Config.SENDER_EMAIL,
    sender_name=Config.SENDER_NAME
)

2. Prepare Email Data

  • Create a recipient email:
    recipient = "[email protected]"
  • Set the email subject:
    subject = "Welcome to Our Service!"
  • Specify the email template (e.g., welcome.html) and provide dynamic data:
    template_name = "welcome"
    template_data = {"username": "John Doe", "verification_link": "https://example.com/verify"}

3. Render the Email Template

email_content = email_service.render_template(template_name, template_data)

4. Send the Email

email_service.send_email(recipients=recipient, subject=subject, html_content=email_content)

Full Example

if __name__ == "__main__":
    email_service = EmailService(
        api_key=Config.RESEND_API_KEY,
        sender_email=Config.SENDER_EMAIL,
        sender_name=Config.SENDER_NAME
    )

    recipient = "[email protected]"
    subject = "Welcome to Our Service!"
    template_name = "welcome"

    try:
        template_data = {"username": "John Doe", "verification_link": "https://example.com/verify"}
        email_content = email_service.render_template(template_name, template_data)
        email_service.send_email(recipients=recipient, subject=subject, html_content=email_content)
    except Exception as e:
        logger.error(f"An error occurred: {str(e)}")

Email Template Example

Save the following HTML template as welcome.html in the templates folder:

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome</title>
  </head>
  <body>
    <h1>Welcome, {{ username }}!</h1>
    <p>
      Thank you for joining our service. Please verify your email address by
      clicking the link below:
    </p>
    <a href="{{ verification_link }}">Verify Email</a>
  </body>
</html>

Logging

This project uses Loguru for logging. Log entries are displayed in the console, including errors and successful email sends.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to submit issues and pull requests.

Acknowledgments

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published