Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email.mime.text import MIMEText
- from email import encoders
- def send_email(message, subject, toaddrs, attach_file):
- fromaddr = 'myemmail@gmail.com'
- username = fromaddr
- password = 'password'
- msg = MIMEMultipart()
- msg['From'] = fromaddr
- msg['To'] = toaddrs
- msg['Subject'] = subject
- body = message
- msg.attach(MIMEText(body, 'html'))
- if attach_file is not False:
- for file in attach_file:
- with open(file, 'rb') as fp:
- process = MIMEBase('application', 'octet-stream')
- process.set_payload(fp.read())
- encoders.encode_base64(process)
- process.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
- msg.attach(process)
- server = smtplib.SMTP('smtp.gmail.com', 587)
- server.starttls()
- server.login(username, password)
- text = msg.as_string()
- server.sendmail(fromaddr, [toaddrs], text)
- server.quit()
- def get_attach_num():
- while True:
- try:
- ask = int(input('Number of attachments >>> '))
- return ask
- except ValueError:
- print('Wrong value entered')
- def yes_no_attach():
- while True:
- try:
- ask_attach = input('Any attachments?nY/N? >>> ').lower()
- if ask_attach == 'y':
- attach_num = get_attach_num()
- return attach_num
- elif ask_attach == 'n':
- return ask_attach
- except ValueError:
- print('Wrong value entered')
- def main():
- attach = []
- subject = input('Subject >>> ')
- toaddrs = input('Address of the recipient >>> ')
- message = input('Your message >>> ')
- ask_attach = yes_no_attach()
- if ask_attach == 'n':
- try:
- send_email(str(message), str(subject), str(toaddrs), attach_file=False)
- print('Email has been sent')
- except smtplib.SMTPRecipientsRefused:
- print('No recipient(s) selected')
- print('Try again')
- main()
- else:
- ask = ask_attach
- for times in range(ask):
- attach.append(input('Path to the file %s that you with to attach >>> ' % str(times + 1)))
- try:
- send_email(str(message), str(subject), str(toaddrs), attach)
- print('Email has been sent')
- except (FileNotFoundError, smtplib.SMTPRecipientsRefused):
- print('No attachment(s)\recipient(s) selected')
- print('Try again')
- main()
- if __name__ == '__main__':
- print('Welcome to SendGmail V2 by AJ')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement