pgcr-mail 0.1.0
用来发送邮件的极简库，支持同步和异步两种方式，可以结合FastAPI使用
License: MIT
Requires Python: >=3.7,<4.0

# pgcr_mail

## 介绍
用来发送邮件的极简库，支持同步和异步两种方式，可以结合FastAPI使用

## 快速入门
## 结合FastAPI使用
### 发送邮件
```python
from fastapi import FastAPI
from pgcr_mail import Mail, Message, Config
from starlette.responses import JSONResponse
from pydantic import EmailStr, BaseModel
from typing import List
import config

setting = config.Settings()


class EmailSchema(BaseModel):
    email: List[EmailStr]


conf = Config(
    MAIL_USERNAME=setting.MAIL_USERNAME,
    MAIL_PASSWORD=setting.MAIL_PASSWORD,
    MAIL_FROM=setting.MAIL_FROM,
    MAIL_PORT=setting.MAIL_PORT,
    MAIL_SERVER=setting.MAIL_SERVER,
    MAIL_FROM_NAME=setting.MAIL_FROM_NAME,
    MAIL_TLS=setting.MAIL_TLS,
    MAIL_SSL=setting.MAIL_SSL,
    USE_CREDENTIALS=setting.USE_CREDENTIALS
)

app = FastAPI()

html = """
<p>这是一封测试邮件，不需要回复</p> 
"""


@app.post("/email")
async def simple_send(
        email: EmailSchema
) -> JSONResponse:
    message = Message(
        subject="测试邮件",
        recipients=email.dict().get("email"), 
        body=html,
        subtype="html"
    )
    fm = Mail(conf)
    await fm.send_message(message)
    return JSONResponse(status_code=200, content={"message": "邮件已发送"})


if __name__ == '__main__':
    import uvicorn

    uvicorn.run("example:app", reload=True)
```

### 使用后台任务发送邮件
```python
from fastapi import (
    FastAPI,
    BackgroundTasks,
)
from pgcr_mail import Mail, Message, Config
from starlette.responses import JSONResponse
from pydantic import EmailStr, BaseModel
from typing import List
import config

setting = config.Settings()


class EmailSchema(BaseModel):
    email: List[EmailStr]


conf = Config(
    MAIL_USERNAME=setting.MAIL_USERNAME,
    MAIL_PASSWORD=setting.MAIL_PASSWORD,
    MAIL_FROM=setting.MAIL_FROM,
    MAIL_PORT=setting.MAIL_PORT,
    MAIL_SERVER=setting.MAIL_SERVER,
    MAIL_FROM_NAME=setting.MAIL_FROM_NAME,
    MAIL_TLS=setting.MAIL_TLS,
    MAIL_SSL=setting.MAIL_SSL,
    USE_CREDENTIALS=setting.USE_CREDENTIALS
)

app = FastAPI()

html = """
<p>这是一封测试邮件，不需要回复</p> 
"""


@app.post("/email")
async def simple_send(
        background_tasks: BackgroundTasks,
        email: EmailSchema
) -> JSONResponse:
    message = Message(
        subject="测试邮件",
        recipients=email.dict().get("email"),
        body=html,
        subtype="html"
    )
    fm = Mail(conf)
    background_tasks.add_task(fm.send_message, message)  # 使用后台任务发送邮件
    return JSONResponse(status_code=200, content={"message": "邮件已发送"})


if __name__ == '__main__':
    import uvicorn

    uvicorn.run("example:app", reload=True)
```

### 携带文件发送
```python
from fastapi import (
    FastAPI,
    BackgroundTasks,
    UploadFile, File,
    Form,
)
from pgcr_mail import Mail, Message, Config
from starlette.responses import JSONResponse
from pydantic import EmailStr, BaseModel
from typing import List
import config

setting = config.Settings()


class EmailSchema(BaseModel):
    email: List[EmailStr]


conf = Config(
    MAIL_USERNAME=setting.MAIL_USERNAME,
    MAIL_PASSWORD=setting.MAIL_PASSWORD,
    MAIL_FROM=setting.MAIL_FROM,
    MAIL_PORT=setting.MAIL_PORT,
    MAIL_SERVER=setting.MAIL_SERVER,
    MAIL_FROM_NAME=setting.MAIL_FROM_NAME,
    MAIL_TLS=setting.MAIL_TLS,
    MAIL_SSL=setting.MAIL_SSL,
    USE_CREDENTIALS=setting.USE_CREDENTIALS
)

app = FastAPI()

html = """
<p>这是一封测试邮件，不需要回复</p> 
"""


@app.post("/email")
async def simple_send(
        background_tasks: BackgroundTasks,
        email: EmailSchema
) -> JSONResponse:
    message = Message(
        subject="测试邮件",
        recipients=email.dict().get("email"),
        body=html,
        subtype="html"
    )
    fm = Mail(conf)
    background_tasks.add_task(fm.send_message, message)  # 使用后台任务发送邮件
    return JSONResponse(status_code=200, content={"message": "邮件已发送"})


@app.post("/file")
async def send_file(
        background_tasks: BackgroundTasks,
        file: UploadFile = File(...),  # 上传文件
        email: EmailStr = Form(...)
) -> JSONResponse:
    message = Message(
        subject="测试",
        recipients=[email],  # 收件人列表
        body="测试邮件发送，携带文件",
        attachments=[file]  # 附件列表
    )
    fm = Mail(conf)
    background_tasks.add_task(fm.send_message, message)
    return JSONResponse(status_code=200, content={"message": "邮件已发送"})


if __name__ == '__main__':
    import uvicorn

    uvicorn.run("example:app", reload=True)
```

### 发送HTML
```python
from fastapi import (
    FastAPI,
    BackgroundTasks,
)
from pgcr_mail import Mail, Message, Config
from starlette.responses import JSONResponse
from pydantic import EmailStr, BaseModel
from typing import List, Dict, Any
import config

setting = config.Settings()


class EmailSchema(BaseModel):
    email: List[EmailStr]
    body: Dict[str, Any]


conf = Config(
    MAIL_USERNAME=setting.MAIL_USERNAME,
    MAIL_PASSWORD=setting.MAIL_PASSWORD,
    MAIL_FROM=setting.MAIL_FROM,
    MAIL_PORT=setting.MAIL_PORT,
    MAIL_SERVER=setting.MAIL_SERVER,
    MAIL_FROM_NAME=setting.MAIL_FROM_NAME,
    MAIL_TLS=setting.MAIL_TLS,
    MAIL_SSL=setting.MAIL_SSL,
    USE_CREDENTIALS=setting.USE_CREDENTIALS
)

app = FastAPI()

html = """
<p>这是一封测试邮件，不需要回复</p>
<h1>OK OK</h1> 
"""


@app.post("/email")
async def simple_send(
        background_tasks: BackgroundTasks,
        email: EmailSchema
) -> JSONResponse:
    message = Message(
        subject="测试邮件",
        recipients=email.dict().get("email"),
        body=html,
        subtype="html"
    )
    fm = Mail(conf)
    background_tasks.add_task(fm.send_message, message)  # 使用后台任务发送邮件
    return JSONResponse(status_code=200, content={"message": "邮件已发送"})


if __name__ == '__main__':
    import uvicorn

    uvicorn.run("example:app", reload=True)
```