Skip to content

Commit

Permalink
Send email using address string
Browse files Browse the repository at this point in the history
  • Loading branch information
yuecen committed Jul 12, 2016
1 parent 05c8337 commit c4cccc8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,20 @@ Here is an example to use following syntax, the file saved to ```examples/market

##### 2. Send Email

In order to send email for many users flexibly, we combine the email list from
result of ge-explore and SendGrid.
In order to send email to many users flexibly, we combine the email list from
result of ge-explore and SendGrid to approach it.

```
ge-sendgrid --api_user <your_sendgrid_api_user_name>
--api_key <your_sendgrid_api_key>
--explore_starred <user_account>/<repo_name>
--template_path <github-email-explorer_folder_path>/examples/marketing_email.txt
--from_email test@example.com
--explore_starred <user_account>/<repo_name>
--list "John <John@example.org>; Peter James <James@example.org>"
```

```--explore_starred``` and ```--list``` are mutually exclusive.

The following image is an real example of email format for ge-sendgrid command.

> <img src="examples/marketing_email.png" width="300">
Expand Down
2 changes: 1 addition & 1 deletion github_email_explorer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.4'
__version__ = '0.0.6'
21 changes: 14 additions & 7 deletions github_email_explorer/cli_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import github_email
from email_handler import send_sendgrid_by_ges
from email_handler import send_sendgrid_by_email_list
from email_handler import get_email_template


Expand All @@ -23,17 +24,21 @@ def __init__(self):
args = p.parse_args()

self.api_key = args.api_key

self.explore_starred = args.explore_starred
if self.explore_starred:
tmp = re.split('/', self.explore_starred)
assert len(tmp) == 2, "repo format is not correct"

self.repo_user, self.repo_name = tmp[0], tmp[1]

self.list = args.list
if (self.list is not None) and (self.explore_starred is not None):
raise ValueError("list and explore_starred is exclusive")

self.template_path = args.template_path
self.subject = args.subject
self.from_email = args.from_email
self.list = args.list
self.client_id = args.client_id if args.client_id else ''
self.client_secret = args.client_secret if args.client_secret else ''

Expand All @@ -55,15 +60,17 @@ def send_email_by_sendgrid():
email_template = get_email_template(sendgrid_cli_args.template_path)

# send email by py-sendgrid
send_sendgrid_by_ges(sendgrid_api_key=sendgrid_cli_args.api_key,
email_template=email_template,
github_user_emails=ges,
from_email=sendgrid_cli_args.from_email,
send_sendgrid_by_ges(github_user_emails=ges,
sendgrid_api_key=sendgrid_cli_args.api_key,
email_template=email_template, from_email=sendgrid_cli_args.from_email,
subject=sendgrid_cli_args.subject)

if sendgrid_cli_args.list:
# TODO send email by list
pass
email_template = get_email_template(sendgrid_cli_args.template_path)
send_sendgrid_by_email_list(email_list=sendgrid_cli_args.list,
sendgrid_api_key=sendgrid_cli_args.api_key,
email_template=email_template, from_email=sendgrid_cli_args.from_email,
subject=sendgrid_cli_args.subject)


if __name__ == '__main__':
Expand Down
29 changes: 22 additions & 7 deletions github_email_explorer/email_handler.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
from github_email import GithubUserEmail

from jinja2 import FileSystemLoader
from jinja2 import Environment

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Email, Content, Mail

from email.utils import parseaddr
import re
import sys


Expand All @@ -21,24 +24,35 @@ def __init__(self):
self.provider = None


def parse_email():
pass
def parse_email(address):
return parseaddr(address)


def send_sendgrid_by_email_list(email_list):
pass
def send_sendgrid_by_email_list(email_list=None, sendgrid_api_key=None, email_template=None, from_email=None, subject=None):
email_list = re.split(';', email_list)
github_user_emails = [GithubUserEmail(parse_email(address)) for address in email_list if len(address.strip()) > 0]

send_sendgrid(github_user_emails=github_user_emails, sendgrid_api_key=sendgrid_api_key,
email_template=email_template, from_email=from_email, subject=subject)

def send_sendgrid_by_ges(sendgrid_api_key=None, email_template=None, github_user_emails=None, from_email=None, subject=None):

def send_sendgrid_by_ges(github_user_emails=None, sendgrid_api_key=None, email_template=None, from_email=None, subject=None):

send_sendgrid(github_user_emails=github_user_emails, sendgrid_api_key=sendgrid_api_key,
email_template=email_template, from_email=from_email, subject=subject)


def send_sendgrid(sendgrid_api_key=None, email_template=None, github_user_emails=None, from_email=None, subject=None):

assert sendgrid_api_key, "SendGrid API key is required"

sg = SendGridAPIClient(apikey=sendgrid_api_key)

from_email = Email(from_email)
subject = subject
for ge in github_user_emails:
to_email = Email(ge.email)
content = Content("text/html", email_template.render(to_name=ge.name))
content = Content("text/html", email_template.render(to_name=ge.name.decode('utf8')))
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())

Expand All @@ -52,4 +66,5 @@ def get_email_template(path):


if __name__ == '__main__':
pass
# print parse_email('test John+github@example.org')
send_sendgrid_by_email_list(' <John@example.org>; Peter James <James@example.org>;')
9 changes: 6 additions & 3 deletions github_email_explorer/github_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@


class GithubUserEmail(object):
def __init__(self):
def __init__(self, *args, **kwargs):
self.g_id = None
self.name = None
self.email = []
self.name = kwargs.get('name', None)
self.email = kwargs.get('email', None)
if len(args) > 0 and (type(args[0]) is tuple):
self.name = args[0][0]
self.email = args[0][1]


class GithubAPIStatus(object):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='github-email-explorer',
description='Tools to get email addresses from repositories that people starred on GitHub, then sent email to many users from the addresses with email template content.',
description='A tool to get email addresses from starred list of repositories on GitHub, and then send email to users from the addresses with email template content.',
version=metadata['version'],

# Author details
Expand Down

0 comments on commit c4cccc8

Please sign in to comment.