Created
August 16, 2018 18:45
-
-
Save ryosuke-hujisawa/6d2d664cb56682a9a5c548cb349f01d5 to your computer and use it in GitHub Desktop.
Send Emails with Gmail using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
from email.mime.text import MIMEText | |
from email.utils import formatdate | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
""" | |
# | |
# | |
# | |
#make function | |
# | |
# | |
# | |
# | |
""" | |
def create_message(from_addr, to_addr, bcc_addrs, subject, body): | |
msg = MIMEText(body, 'html') | |
msg['Subject'] = subject | |
msg['From'] = from_addr | |
msg['To'] = to_addr | |
msg['Bcc'] = bcc_addrs | |
msg['Date'] = formatdate() | |
return msg | |
def send(from_addr, to_addrs, msg): | |
smtpobj = smtplib.SMTP('smtp.gmail.com', 587) | |
smtpobj.ehlo() | |
smtpobj.starttls() | |
smtpobj.ehlo() | |
smtpobj.login(FROM_ADDRESS, MY_PASSWORD) | |
smtpobj.sendmail(from_addr, to_addrs, msg.as_string()) | |
smtpobj.close() | |
""" | |
# | |
# | |
# | |
#Let's send email | |
# | |
# | |
# | |
# | |
""" | |
TO_ADDRESS = '' | |
FROM_ADDRESS = '' | |
MY_PASSWORD = '' | |
BCC = '' | |
SUBJECT = 'SUBJECT' | |
BODY = """\ | |
<html> | |
<head></head> | |
<body> | |
<h1>h1 text</h1> | |
<p>p text</p> | |
</html> | |
""" | |
msg = create_message(FROM_ADDRESS, TO_ADDRESS, BCC, SUBJECT, BODY) | |
send(FROM_ADDRESS, TO_ADDRESS, msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment