Skip to content

Commit

Permalink
final commit before release: 0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
marktennyson committed Sep 23, 2021
1 parent 09f9390 commit e007eb0
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 53 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ ipython_config.py
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

#extra files
test.py


# Celery stuff
celerybeat-schedule
celerybeat.pid
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"git.ignoreLimitWarning": true
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
## 0.0.5
- Added one more config variable named `MAIL_DEFAULT_SENDER`. It's as same as `MAIL_FROM` config var.
- Fixed absent of `httpx` module at the __setup.py__ file.
- Config var `MAIL_SSL` and `MAIL_TLS` changes to `MAIL_USE_SSL` and `MAIL_USE_TLS` accordingly.
- added `add_recipient` and `attach` method to the __schemas.Message__ class.
- Fixed some broken test cases.
- modifications at the documentation.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-exclude flask-mailing/examples *
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Flask-Mailing
![Flask mail logo](https://github.com/marktennyson/flask-mailing/blob/main/flask-mailing-logo-cropped.png?raw=true)

Flask-Mailing adds SMTP mail sending to your Flask applications

Flask_Mail is dead now. To use the mail service with your project you can use eaither [Flask-Mailing](https://github.com/marktennyson/flask-mailing) for legacy or [Flask-Mailman](https://github.com/waynerv/flask-mailman) for Django type implementation.

Flask-Mailing is a fork of `Sabuhi's` Fastapi-Mail package, providing similar functionality. 99% of the work was done by him, and the fork was made mainly provide the same features and the apis for the Flask Microframework.

##### Need help to create and deploy the test cases.(Urgent)
# Downloads
[![Downloads](https://pepy.tech/badge/flask-mailing)](https://pepy.tech/project/flask-mailing) [![Downloads](https://pepy.tech/badge/flask-mailing/month)](https://pepy.tech/project/flask-mailing) [![Downloads](https://pepy.tech/badge/flask-mailing/week)](https://pepy.tech/project/flask-mailing)
<br>


### 🔨 Installation ###

Expand Down Expand Up @@ -48,8 +53,8 @@ app.config['MAIL_USERNAME'] = "YourUserName"
app.config['MAIL_PASSWORD'] = "strong_password"
app.config['MAIL_PORT'] = 587
app.config['MAIL_SERVER'] = "your mail server"
app.config['MAIL_TLS'] = True
app.config['MAIL_SSL'] = False
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
app.config['USE_CREDENTIALS'] = True
app.config['VALIDATE_CERTS'] = True
app.config['MAIL_DEFAULT_SENDER'] = "[email protected]"
Expand Down
16 changes: 14 additions & 2 deletions docs/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def create_app():
app.config['MAIL_PASSWORD'] = "world_top_secret_password"
app.config['MAIL_PORT'] = 587
app.config['MAIL_SERVER'] = "your-email-server.com"
app.config['MAIL_TLS'] = True
app.config['MAIL_SSL'] = False
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
mail.init_app(app)

return app
Expand All @@ -45,6 +45,13 @@ async def simple_send():
return jsonify(status_code=200, content={"message": "email has been sent"})
```

#### Add recipient using `add_recipient` method

```python
message.add_recipient("[email protected]")
```


### Send a simple html message
```python

Expand Down Expand Up @@ -81,6 +88,11 @@ async def mail_file():
await mail.send_message(message)
return jsonify(message="email sent")
```
#### Sending files using `attach` method
```python
with app.open_resource("attachments/example.txt") as fp:
message.attach("example.txt", fp.read())
```

### Using Jinja2 HTML Templates

Expand Down
8 changes: 4 additions & 4 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class has following attributes
- If you service does not provide username use sender address for connection.
- **MAIL_PASSWORD** : Password for authentication
- **MAIL_SERVER** : SMTP Mail server.
- **MAIL_TLS** : For TLS connection
- **MAIL_SSL** : For TLS connection
- **MAIL_USE_TLS** : For TLS connection
- **MAIL_USE_SSL** : For TLS connection
- **MAIL_DEBUG** : Debug mode for while sending mails, defaults 0.
- **MAIL_FROM** : Sender address
- **MAIL_DEFAULT_SENDER** : Sender address
Expand Down Expand Up @@ -44,10 +44,10 @@ class has following attributes
- reply_to : Reply-To recipients in the mail
- charset : charset defaults to utf-8
- subtype : subtype of the mail defaults to plain
- add_recipient : a method to add additional recipients.
- attach : a method to add additional attachments.




### ```utils.DefaultChecker``` class
Default class for checking email from collected public resource.
The class makes it possible to use redis to save data.
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def create_app():
app.config['MAIL_PASSWORD'] = "world_top_secret_password"
app.config['MAIL_PORT'] = 587
app.config['MAIL_SERVER'] = "your-email-server.com"
app.config['MAIL_TLS'] = True
app.config['MAIL_SSL'] = False
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
app.config['MAIL_DEFAULT_SENDER'] = "[email protected]"
mail.init_app(app)

Expand Down
14 changes: 8 additions & 6 deletions examples/base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from flask import Flask
import os as os


def create_app():
app = Flask(__name__)


app.config['MAIL_USERNAME'] = "[email protected]"
app.config['MAIL_PASSWORD'] = ""
app.config['MAIL_USERNAME'] = os.environ['mu']
app.config['MAIL_PASSWORD'] = os.environ['mp']
app.config['MAIL_FROM'] = "[email protected]"
app.config['MAIL_PORT'] = 587
app.config['MAIL_SERVER'] = "smtp.gmail.com"
app.config['MAIL_TLS'] = True
app.config['MAIL_SSL'] = False
app.config['MAIL_PORT'] = os.environ['mport']
app.config['MAIL_SERVER'] = os.environ['ms']
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
# app.config['MAIL_TEMPLATE_FOLDER'] = Path(__file__).parent / 'attachments'

return app
15 changes: 11 additions & 4 deletions examples/application.py → examples/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from dotenv import load_dotenv; load_dotenv()

from flask import jsonify
from base import create_app
from flask_mailing import Mail, Message

import os as os

mail = Mail()


Expand All @@ -24,10 +28,10 @@ async def simple_send() -> jsonify:

message = Message(
subject="Flask-Mailing module",
recipients=["[email protected]"],
recipients=[os.environ['MAIL_RECIPIENT']],
body="This is the basic email body",
# subtype="html"
)
message.add_recipient("[email protected]")


await mail.send_message(message)
Expand All @@ -38,10 +42,13 @@ async def simple_send() -> jsonify:
async def mail_file():
message = Message(
subject = "attachments based email",
recipients = ["[email protected]"],
recipients = [os.environ['MAIL_RECIPIENT']],
body = "This is the email body",
attachments = ['attachments/attachment.txt']
)
with app.open_resource('attachments/test.html') as fp:
message.attach("test.html", fp.read())

await mail.send_message(message)
return jsonify(message="email sent")

Expand All @@ -50,7 +57,7 @@ async def mail_html():

message = Message(
subject = "html template based email",
recipients = ["[email protected]"],
recipients = [os.environ['MAIL_RECIPIENT']],
template_body = {
"first_name": "Fred",
"last_name": "Fredsson"
Expand Down
9 changes: 3 additions & 6 deletions flask_mailing/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ class Mail(_MailMixin):
"""

def __init__(self,
app:t.Optional["Flask"]=None
# config: ConnectionConfig
):
def __init__(self, app:t.Optional["Flask"]=None) -> None:

if app is not None:
self.init_app(app)
Expand All @@ -65,8 +62,8 @@ def init_app(self, app:"Flask") -> None:
MAIL_PASSWORD = app.config.get("MAIL_PASSWORD"),
MAIL_PORT = app.config.get("MAIL_PORT", 465),
MAIL_SERVER = app.config.get("MAIL_SERVER"),
MAIL_TLS = app.config.get("MAIL_TLS", False),
MAIL_SSL = app.config.get("MAIL_SSL", True),
MAIL_TLS = app.config.get("MAIL_USE_TLS", False),
MAIL_SSL = app.config.get("MAIL_USE_SSL", True),
MAIL_DEBUG= app.config.get("MAIL_DEBUG", 0),
MAIL_FROM_NAME = app.config.get("MAIL_FROM_NAME",None),
MAIL_TEMPLATE_FOLDER = app.config.get("MAIL_TEMPLATE_FOLDER",None),
Expand Down
14 changes: 10 additions & 4 deletions flask_mailing/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
from email.utils import formatdate, make_msgid
from email.encoders import encode_base64

import typing as t

if t.TYPE_CHECKING:
from werkzeug.datastructures import FileStorage

PY3 = sys.version_info[0] == 3


Expand Down Expand Up @@ -40,8 +45,7 @@ def _mimetext(self, text, subtype="plain"):

return MIMEText(text, _subtype=subtype, _charset=self.charset)

async def attach_file(self, message, attachment):

async def attach_file(self, message, attachment:t.List["FileStorage"]):
for file in attachment:

part = MIMEBase(_maintype="application", _subtype="octet-stream")
Expand All @@ -58,14 +62,16 @@ async def attach_file(self, message, attachment):
filename = filename.encode('utf8')

filename = ('UTF8', '', filename)

disposition:str = getattr(file, 'disposition', 'attachment')
part.add_header(
'Content-Disposition',
"attachment",
disposition,
filename=filename)

self.message.attach(part)

file.close() # close the file stream

async def _message(self, sender):
"""Creates the email message"""

Expand Down
52 changes: 51 additions & 1 deletion flask_mailing/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os
import io
from enum import Enum
from typing import List, Union, Any, Optional, Dict
from typing import (
List,
Union,
Any,
Optional,
Dict,
Literal
)
from mimetypes import MimeTypes

from pydantic import BaseModel, EmailStr, validator
Expand Down Expand Up @@ -68,6 +76,48 @@ def validate_subtype(cls, value, values, config, field):
return 'html'
return value


def add_recipient(self, recipient:str) -> Literal[True]:
"""
Adds another recipient to the message.
:param recipient: email address of recipient.
"""
self.recipients.append(recipient)
return True


def attach(
self,
filename:str,
data:Union[bytes,str],
content_type:dict=None,
disposition:str='attachment',
headers:dict={}
) -> Literal[True]:
"""
Adds an attachment to the message.
:param `filename`: filename of attachment
:param `data`: the raw file data
:param `content_type`: file mimetype
:param `disposition`: content-disposition (if any)
:param `headers`: dictionary of headers
"""
if content_type is None:
mime = MimeTypes()
content_type = mime.guess_type(filename)

fsob:"FileStorage" = FileStorage(
io.BytesIO(data),
filename,
content_type=content_type,
headers=headers
)
fsob.disposition = disposition
self.attachments.append(fsob)
return True


def validate_path(path):
cur_dir = os.path.abspath(os.curdir)
Expand Down
Binary file added logo/flask-mailing-logo-cropped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logo/flask-mailing-logo-full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
site_name: Flask-MAILING
site_url:
site_url: https://marktennyson.github.io/flask-mailing
site_description: flask-mailing
dev_addr: 127.0.0.1:8080

Expand All @@ -15,9 +15,9 @@ theme:



repo_name: flask-mailing
repo_name: marktennyson/flask-mailing
repo_url: https://github.com/marktennyson/flask-mailing
edit_uri: ''
edit_uri: 'https://github.com/marktennyson/flask-mailing'

plugins:
- search
Expand Down
16 changes: 10 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from setuptools import setup,find_packages
from setuptools import (
setup,
find_packages
)

version = (0, 0, 5)
author = "Aniket Sarkar"
VERSION = (0, 0, 5)
AUTHOR = "Aniket Sarkar"
AUTHOR_EMAIL = "[email protected]"


with open("README.md", "r") as f:
Expand All @@ -10,11 +14,11 @@

setup(
name="Flask-Mailing",
version=".".join([str(i) for i in list(version)]),
version=".".join([str(i) for i in list(VERSION)]),
url="https://github.com/marktennyson/flask-mailing",
license="MIT",
author=author,
author_email="[email protected]",
author=AUTHOR,
author_email=AUTHOR_EMAIL,
description="Flask mail system sending mails(individual, bulk) attachments(individual, bulk) fully asynchroniously",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
Loading

0 comments on commit e007eb0

Please sign in to comment.