Created
December 26, 2020 16:09
-
-
Save HH0718/6fa22658db1928880af3994682a306d2 to your computer and use it in GitHub Desktop.
mail-client help request from /u/dingus45191 post: https://www.reddit.com/r/learnpython/comments/kkjfhy/got_into_a_hell_lot_of_errors_while_making_a_mail/
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
[CREDENTIALS] | |
EMAIL = "my.email@gmail.com" | |
PASSWORD = "MySuperSecretPassword" | |
[SERVER] | |
SMTP = "smtp.gmail.com" | |
PORT = 587 |
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 import encoders | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
from email.mime.multipart import MIMEMultipart | |
from vyper import v | |
v.set_config_name('config') | |
v.add_config_path('.') | |
v.read_in_config() | |
def send_email(recipient, subject): | |
credentials = v.get('CREDENTIALS') | |
server = v.get('SERVER') | |
if not credentials['EMAIL'] and not credentials['PASSWORD']: | |
print("Missing credentials in config file.") | |
quit() | |
server = smtplib.SMTP(server['SMTP'], server['PORT']) | |
server.starttls() | |
server.ehlo() | |
server.login(credentials['EMAIL'], credentials['PASSWORD']) | |
msg = MIMEMultipart() | |
msg["From"] = credentials['EMAIL'] | |
msg["To"] = recipient | |
msg["Subject"] = subject | |
with open("message.txt", "r") as f: | |
message = f.read() | |
msg.attach(MIMEText(message)) | |
filename = "photo.jpg" | |
attachment = open(filename, "rb") | |
p = MIMEBase("application", "octet-stream") | |
p.set_payload(attachment.read()) | |
encoders.encode_base64(p) | |
p.add_header("Content-Deposition", 'filename') | |
text = msg.as_string() | |
server.sendmail(credentials['EMAIL'], recipient, text) | |
if __name__ == "__main__": | |
# Change `config.toml.example` to `config.toml` then enter data. | |
# call send_email() and enter recipient's email and subject as args. | |
send_email("mubashirhasan2005@outlook.com", "The Subject") |
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
Well I'm making my own gmail client | |
Regards, | |
Mohammed Mubashir Hasan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment