Metadata-Version: 2.1
Name: smtpymailer
Version: 0.1.2
Summary: A emailing python library for emailing from alternative domains - DNS entries etc need to be assigned (not used for spamming).
Home-page: https://github.com/lewis-morris/smtpymailer
Author: Lewis Morris
Author-email: lewis@arched.dev
License: MIT
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.7
Description-Content-Type: text/markdown
Requires-Dist: beautifulsoup4==4.12.3
Requires-Dist: certifi==2023.11.17
Requires-Dist: cffi==1.16.0
Requires-Dist: charset-normalizer==3.3.2
Requires-Dist: coverage==7.4.1
Requires-Dist: coverage-badge==1.1.0
Requires-Dist: cryptography==42.0.1
Requires-Dist: dmarc==1.0.3
Requires-Dist: dnspython==2.5.0
Requires-Dist: docutils==0.20.1
Requires-Dist: email-validator==2.1.0.post1
Requires-Dist: html2text==2020.1.16
Requires-Dist: idna==3.6
Requires-Dist: importlib-metadata==7.0.1
Requires-Dist: iniconfig==2.0.0
Requires-Dist: jaraco.classes==3.3.0
Requires-Dist: jeepney==0.8.0
Requires-Dist: Jinja2==3.1.3
Requires-Dist: keyring==24.3.0
Requires-Dist: lxml==5.1.0
Requires-Dist: mailinator-python-client-2==0.0.4
Requires-Dist: markdown-it-py==3.0.0
Requires-Dist: MarkupSafe==2.1.4
Requires-Dist: mdurl==0.1.2
Requires-Dist: more-itertools==10.2.0
Requires-Dist: nh3==0.2.15
Requires-Dist: packaging==23.2
Requires-Dist: pillow==10.2.0
Requires-Dist: pkginfo==1.9.6
Requires-Dist: pluggy==1.4.0
Requires-Dist: pycparser==2.21
Requires-Dist: pydig==0.4.0
Requires-Dist: Pygments==2.17.2
Requires-Dist: pytest==8.0.0
Requires-Dist: python-dotenv==1.0.1
Requires-Dist: python-magic==0.4.27
Requires-Dist: readme-renderer==42.0
Requires-Dist: requests==2.31.0
Requires-Dist: requests-toolbelt==1.0.0
Requires-Dist: rfc3986==2.0.0
Requires-Dist: rich==13.7.0
Requires-Dist: SecretStorage==3.3.3
Requires-Dist: soupsieve==2.5
Requires-Dist: twine==4.0.2
Requires-Dist: urllib3==2.1.0
Requires-Dist: validators==0.22.0
Requires-Dist: zipp==3.17.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: email-validator; extra == "dev"
Requires-Dist: mailinator-python-client-2==0.0.4; extra == "dev"
Requires-Dist: coverage; extra == "dev"
Requires-Dist: coverage-badge; extra == "dev"


# smtpymailer

[![Python Unittest](https://github.com/arched-dev/smtpymailer/actions/workflows/main.yml/badge.svg)](https://github.com/arched-dev/smtpymailer/actions/workflows/main.yml)

[![Python Coverage](https://github.com/arched-dev/smtpymailer/blob/master/tests/assets/coverage.svg)](https://github.com/arched-dev/smtpymailer/actions/workflows/main.yml)



## Introduction
`smtpymailer` is a versatile Python library designed to simplify the process of sending emails in python. It supports
and validates DKIM, DMARC, and SPF records before sending emails to avoid being marked as spam, so it is ideal for sending 
emails from your own mail server but the sender is an alternative domain.


## Features
- Login to any mail server of your choice (I maintain a postfix server for outgoing mail only, so replies are still directed to the users normal inbox).
- Send emails from different domains, provided the correct DNS settings are in place.
- Validates DNS settings like DKIM, DMARC, and SPF before sending emails to avoid being marked as spam.
- Full email functionality including `To`, `CC`, `BCC`, `Reply-To`, and attachments.
- Adds attachments with the correct MIME type.
- Optionally converts url <img> tags to inline attachments CID attachments or Base64 encoded img sources, this is controlled by data attributes `data-inline` or `data-base` in the img tag.
- Supports sending string HTML content, Jinja templates, or plain html files.
- Automatically converts HTML to plain text for email clients that do not support HTML.
- Supports environment variables and `.env` files for mail server settings.

## Installation
Install the package using `pip`:

```bash
pip install smtpymailer
```

## Usage

### Initialization

Create an instance of `SmtpyMailer` with your sender email and name. The mail server details can be passed as keyword
arguments or sourced from environment variables or a `.env` file.

### Environment Variables

The following environment variables can be set or added to a `.env` file in your project root.
Alternatively you can pass as **kwargs to the `SmtpyMailer` class but this is not advised.

- `MAIL_SERVER`: The mail server hostname.
- `MAIL_PORT`: The mail server port.
- `MAIL_USE_TLS`: Whether to use TLS or not.
- `MAIL_USERNAME`: The mail server username.
- `MAIL_PASSWORD`: The mail server password.
- `MAIL_DKIM_SELECTOR`: The DKIM selector of the domain you are sending from. (This is used to validate your ability to send from the domain).


```python
from smtpymailer import SmtpMailer
mailer = SmtpMailer(sender_email="foo@bar.com", sender_name="Foo Bar")
```

### Sending an Email

Use the `send_email` method to send emails:


#### Parameters

- `recipients`: Single recipient email or a list of email addresses.
- `subject`: Email subject.
- `cc_recipients`: (Optional) Single or list of CC email addresses.
- `bcc_recipients`: (Optional) Single or list of BCC email addresses.
- `reply_to`: (Optional) Reply-to email address.
- `attachments`: (Optional) Single file path or list of file paths for attachments.
- `html_content`: (Optional) HTML content of the email (not required if using a template)
- `template`: (Optional) Template file path (using jinja), it can be just a plain html file.
- `template_directory`: (Optional) Single or list of template directories.
- `**kwargs`: Additional arguments for jinja template, if needed.

### Example

- Send a simple email with a subject and html content.

```python
from smtpymailer import SmtpMailer
mailer = SmtpMailer(sender_email="foo@bar.com", sender_name="Foo Bar")
mailer.send_email(recipients="bar@baz.com", subject="My test email", html_content="<h1>Hello World</h1>")
```

- Send to multiple recipients, one CC, with an attachment, and a jinja template with kwargs.

```python
from smtpymailer import SmtpMailer
mailer = SmtpMailer(sender_email="foo@bar.com", sender_name="Foo Bar")
template_data = {"name": "Foo Bar", "message": "Hello Foo", "url": "https://www.foo.com"}
mailer.send_email(recipients=["bar@baz.com", "baz@bar.com"], cc_recipients="foo@baz.com", subject="My test email", template="template.html", template_directory="./templates", **template_data)
```
## Sending From Alternative Domains

You can send emails from alternative domains by setting up the correct DNS settings. Here's how to do it.
[Alt Domains Documentation](./docs/ALT_DOMAINS.md)

## License
This project is licensed under the [MIT License](LICENSE).

## TODO
- Change the way the send function's `alter_img_src` parameter works. It should AUTODETECT img elements with `data-inline` or `data-base` attributes instead.
****
