├── .gitignore ├── LICENSE ├── README.md ├── main.py ├── requirements.txt └── src ├── __init__.py └── send_email.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Deepak Raj 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![header](https://capsule-render.vercel.app/api?type=wave&color=gradient&height=300§ion=header&text=PyEmailer&fontSize=50) 2 | 3 | [![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-360/) 4 | ![issues](https://img.shields.io/github/issues/codePerfectPlus/PyEmailer?style=plastic) 5 | ![forks](https://img.shields.io/github/forks/codePerfectPlus/PyEmailer) 6 | ![stars](https://img.shields.io/github/stars/codePerfectPlus/PyEmailer) 7 | ![License](https://img.shields.io/github/license/codePerfectPlus/PyEmailer) 8 | 9 | ![Visitor Count](https://profile-counter.glitch.me/PyEmailer/count.svg) 10 | 11 | **Blog On Python, Machine Learning and Data Science Visit [CodePerfectPLus](http://codeperfectplus.herokuapp.com/)** 12 | 13 | ## Create App Password in gmail. 14 | 15 | - GO to Account [setting/Security](https://myaccount.google.com/security) 16 | - click app password 17 | - Select APP -> others, Select Device -> Others 18 | - Copy paste the code in script.py `password` variable 19 | 20 | ## Usage 21 | 22 | ```bash 23 | git clone https://github.com/codePerfectPlus/PyEmailer 24 | cd PyEmailer 25 | ``` 26 | 27 | ```python 28 | from src.send_email import PyEmailer 29 | 30 | your_email_id = "your_email_id" 31 | your_app_password = "your_app_password" 32 | email_subject = "email_subject_here" 33 | email_content = "

Email Content can be html too

" 34 | listOfEmail = ["destination1@gmail.com", "destination2@gmail.com"] 35 | 36 | pyemail = PyEmailer(your_email_id, your_app_password) 37 | 38 | if __name__ == "__main__": 39 | pyemail.sendEmail(email_subject, email_content, listOfEmail) 40 | ``` 41 | 42 | ## Upcoming features 43 | 44 | - file attachment in email 45 | - ~~RegEx to verify the Emails~~ 46 | 47 | ### Project 48 | 49 | - Project: PyEmailer 50 | - Author: CodePerfectPlus 51 | - Language: Python 52 | - Github: https://github.com/codePerfectPlus 53 | - Website: http://codeperfectplus.herokuapp.com/ 54 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from src.send_email import PyEmailer 2 | 3 | your_email_id = "your_email_id" 4 | your_app_password = "your_app_password" 5 | email_subject = "email_subject_here" 6 | email_content = "

Email Content can be html too

" 7 | listOfEmail = ["destination1@gmail.com", "destination2@gmail.com"] 8 | 9 | pyemail = PyEmailer(your_email_id, your_app_password) 10 | 11 | if __name__ == "__main__": 12 | pyemail.sendEmail(email_subject, email_content, listOfEmail) 13 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Py-Contributors/PyEmailer/36abb003d200e4c184a610cfd76ec8d448eed970/requirements.txt -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Py-Contributors/PyEmailer/36abb003d200e4c184a610cfd76ec8d448eed970/src/__init__.py -------------------------------------------------------------------------------- /src/send_email.py: -------------------------------------------------------------------------------- 1 | import smtplib 2 | import email.message 3 | 4 | import re 5 | 6 | server = smtplib.SMTP('smtp.gmail.com:587') 7 | # regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' 8 | regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' 9 | 10 | 11 | def checkEmail(email): 12 | if re.search(regex, email): 13 | return True 14 | else: 15 | return False 16 | 17 | 18 | class PyEmailer(object): 19 | """ 20 | Class to send email using python 21 | 22 | methods: 23 | sendEmail: Send email to list of email id 24 | 25 | """ 26 | def __init__(self, your_email_id, your_app_password): 27 | self.your_email_id = your_email_id 28 | self.your_app_password = your_app_password 29 | 30 | def sendEmail(self, email_subject: str, 31 | email_content: str, listOfEmail: list) -> dict: 32 | 33 | """ Send email to list of email id 34 | 35 | Args: 36 | email_subject: Subject of email 37 | email_content: Content of email 38 | listOfEmail: List of email id to send email 39 | """ 40 | output = {'success': 0, 'failed': 0, 'invalid': 0} 41 | 42 | msg = email.message.Message() 43 | msg['Subject'] = email_subject 44 | msg['From'] = self.your_email_id 45 | password = self.your_app_password 46 | 47 | msg.add_header('Content-Type', 'text/html') 48 | msg.set_payload(email_content) 49 | server = smtplib.SMTP('smtp.gmail.com: 587') 50 | server.starttls() 51 | 52 | server.login(msg['From'], password) 53 | 54 | # sending email one by one to each email ID in the list 55 | for destinationEmail in listOfEmail: 56 | if checkEmail(destinationEmail): 57 | try: 58 | server.sendmail(msg['From'], destinationEmail, msg.as_string()) 59 | print("sending to {}".format(destinationEmail)) 60 | output['success'] += 1 61 | except Exception as e: 62 | print("Error: {}".format(e)) 63 | output['failed'] += 1 64 | else: 65 | print("[INFO]: {} is not a valid email.".format(destinationEmail)) 66 | output['invalid'] += 1 67 | 68 | return output 69 | --------------------------------------------------------------------------------