Git Product home page Git Product logo

pure-peace / mailservice Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 5.0 522 KB

基于python3的邮件服务。提供邮件模板系统、多语言邮件、自动翻译、多邮件服务器管理以及微型web api示例。 Mail service based on python3. Provide mail template system, multi-language, automatic translation, multi-mail server management and micro web api examples。

License: MIT License

Python 90.82% HTML 9.18%

mailservice's Introduction

🔥 mailService

基于python3的邮件服务(SMTP)。提供邮件模板系统、多语言、自动翻译、多邮件服务器以及微型web api示例。 Mail service based on python3 (SMTP). Provide mail template system, multi-language, automatic translation, multi-mail server management and micro web api examples。

🍖Easy:

  • 🍊Based on python3, and lightweight
  • 🍉Allow to add multiple sending servers, if one server is broken, it will automatically switch to other servers
  • 🍩Email html template system (jinja2 syntax, the same way as flask)
  • 🌼Multi-language support, e-mail in any language
  • 🌠A fairly good translator, add .json translation file and eat it
  • 🍖Multilingual translation generator, automatically generates translations in other languages based on the main language
  • Provide a simple flask example, try to use the web interface to call this mail service

🏆-Start-

  1. 🍬Clone this repository
git clone https://github.com/Pure-Peace/mailService

2、🍙Install dependencies (just flask、py-google-translate...)

pip install -r requirements.txt

3、🍮Add mail server (emailManager.py)

Add your SMTP mail server!

class EmailManager:
    def __init__(self, template_dir='templates', template_extension='.html', translator=None):
        logger('Initialize the mail manager...')
        self.send_success = 0
        self.send_failed = 0
        self.template_dir = template_dir
        self.template_extension = template_extension
        self.templates = {}
        self.mail_servers = []
        self.init_servers()
        self.init_templates()
        self.translator = translator or Translator()
        logger('Initialize mail manager: successful!')
        
    # this way!
    def init_servers(self):
        self.mail_servers = [
            MailServer('aliyun', 'your(sender)@email.com', 'password', 'smtpdm.aliyun.com', 80),
            MailServer('tencent_company', 'your(sender)@email.com', 'password', 'smtp.exmail.qq.com', 465)
        ]
        logger('Initialize the mail server({})..success!'.format(len(self.mail_servers)))
    ...
  1. 🌽try [send email](main.py)

use mailService.send_mail() to send mail

class Handlers:
   def __init__(self):
       self.translator = Translator()
       self.mailService = EmailManager(translator=self.translator)
   
   def reg_success(self, data, recipient, lang='cn'):
       self.translator.language = lang
       title = self.translator.tran('main.reg_success_title').format(data['username'])
           
       return self.mailService.send_mail(recipient, title, content='reg_success', template_data=data, lang=lang)


h = Handlers()


if __name__ == '__main__':
   # send mail to [email protected]
   h.reg_success({'username': 'test', 'usermail': '[email protected]'}, '[email protected]')
   h.reg_success({'username': 'test', 'usermail': '[email protected]'}, '[email protected]', 'en') # you can change language!!
  1. 🍁try [generate translation file](makeTranslations.py)
python makeTranslations.py
  1. 🍭try [use web api send mail] (simpleApi.py)
python simpleApi.py

then open

http://localhost:8898/

lets try

📷Features Show:

1. Multi-language mail, and a randomly-transformed mail color demo (material color style)

This is about the function of email template rendering, located in emailManager.py, I have built several sets of color matching

screenshot

The language of the e-mail can be selected by the webpage, determine the user's language, and submit the backend to send the e-mail in the corresponding language.

screenshot


2. Email template system supporting multiple languages (jinja2 syntax)

Basically the same writing as flask html, the multi-language function is shown in the red box, please see the screenshot of point 1 for the effect of email sending: 1. Multi-language email, and the randomly changed email color (material color style)

Create a translation directory for the corresponding language in the translations directory, and add a translation file in .json format to call multiple languages as described in the picture.

Moreover, you can use multiple languages wherever Python can reach it, as long as you add the translation file and call the tran () method of the translator object correctly.

Calling the template to send mail is very simple, you only need to call the send_mail() method of EmailManager, and set the content parameter to the name of the html template (such as reg_success), the mail system will automatically be in the templates directory Find the corresponding html mail template and render it.

Note that if you want to add data to your html mail template, you also need to pass in template_data data, which is a dictionary.

screenshot


3. Simple to use, allows you to add multiple mail servers, supports SSL (note the content in the red box on the screenshot)

To add a server, just instantiate the MailServer object as described in the figure and add it to the list.

The MailServer object is very simple, see mailServer.py, mainly for some configuration of the sending server, contains the reply address reply_address, and client_type not shown in the figure (used to indicate whether it is ssl) Another important thing is sender_name, which will be displayed in the emails received by users.

screenshot


4. Translator, the default language is simplified Chinese(cn)

Translation can be achieved by changing language to the English abbreviation in the specified language. If the translator cannot find a translation in the specified language, it will automatically select the default_language language, so please make sure that default_language is at least complete.

At the same time, you need to add translation files in translation to support the translator. The following provides a small tool that automatically generates a Google translated .json translation file.

screenshot


5. Translation file in json format

This is the translation file in json format.

For the specific calling method, please turn up to point 2: 2. Multi-language mail template system (jinja2 syntax).

screenshot


6. Provide a small tool to create multi-language translations with one click [makeTranslations.py]

This tool creates a multilingual translation file by calling Python's Google Translate API.

First of all, you must ensure that the language specified in the language configuration of translator.py is complete before you can use this tool.

It will use language of the translator object as the basic language, and then generate translation files in other languages based on the translation files in the basic language.

screenshot

Edit makeTranslations.py and run it directly to create a language directory in the translations directory and generate translation files in json format.

very simple

screenshot


8. Provide web interface call demo (flask) [simpleApi.py]

This is very simple, you can see the effect by running simpleApi.py

For example, when you visit http://localhost:8898/send_mail/reg_success/[email protected]/test?lang=en, the mail service will send the user test <[email protected]> a template with reg_success email, The email language is lang=en.

This means that you can easily send emails in the corresponding language according to the user's browser language, just call the API.

screenshot


enjoy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.