This is a class for setting outgoing mail. Set the sender, destination, and gmail account passwords in the initialization, Set the content of the argument of the send method.
send_message.py
Learn more or give us feedback
from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib
class Send_Message:
fromaddress = '[email protected]'
toaddress = '[email protected]'
password = 'password'
def __init__(self, fromaddress, toaddress, password):
self.fromaddress = fromaddress
self.toaddress = toaddress
self.password = password
def send(self, content):
smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
smtpobj.ehlo()
smtpobj.starttls()
smtpobj.ehlo()
smtpobj.login(self.fromaddress, self.password)
msg = MIMEText(content)
msg['Subject'] = 'subject'
msg['From'] = self.fromaddress
msg['To'] = self.toaddress
msg['Date'] = formatdate()
smtpobj.sendmail(self.fromaddress, self.toaddress, msg.as_string())
smtpobj.close()
It will be the main process. Here, set the source, destination, password, content, If you execute it, you will be logged in to gmail and an email will be sent.
mail.py
from send_message import Send_Message
import sys
def main():
fromaddress = '@gmail.com'
toaddress = '@gmail.com'
password = ''
content =""
try:
mail = Send_Message(fromaddress, toaddress, password)
mail.send(content)
print("success")
except:
print("error")
if __name__ == "__main__":
main()
I have the source on github. https://github.com/kurihiro0119/gmail_send_API
Recommended Posts