Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import httplib2
- import os, glob,time
- from PyPDF2 import PdfFileReader, PdfFileWriter
- import oauth2client
- from oauth2client import client, tools
- import base64
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from apiclient import errors, discovery
- import mimetypes
- from email.mime.image import MIMEImage
- from email.mime.audio import MIMEAudio
- from email.mime.base import MIMEBase
- from email import encoders
- def pdf_splitter(path):
- fname = os.path.splitext(os.path.basename(path))[0]
- pdf = PdfFileReader(path)
- for page in range(pdf.getNumPages()):
- pdf_writer = PdfFileWriter()
- pdf_writer.addPage(pdf.getPage(page))
- output_filename = 'temp_{}_page_{}.pdf'.format(
- fname, page+1)
- with open(output_filename, 'wb') as out:
- pdf_writer.write(out)
- print('Created: {}'.format(output_filename))
- def pdf_temp_del():
- for filename in glob.glob("./temp*"):
- os.remove(filename)
- SCOPES = 'https://www.googleapis.com/auth/gmail.send'
- CLIENT_SECRET_FILE = 'client_secret.json'
- def get_credentials():
- home_dir = os.path.expanduser('~')
- credential_dir = os.path.join(home_dir, '.credentials')
- if not os.path.exists(credential_dir):
- os.makedirs(credential_dir)
- credential_path = os.path.join(credential_dir,
- 'gmail-python-email-send.json')
- store = oauth2client.file.Storage(credential_path)
- credentials = store.get()
- if not credentials or credentials.invalid:
- flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
- flow.user_agent = APPLICATION_NAME
- credentials = tools.run_flow(flow, store)
- print('Storing credentials to ' + credential_path)
- return credentials
- def SendMessageInternal(service, user_id, message):
- try:
- message = (service.users().messages().send(userId=user_id, body=message).execute())
- print('Message Id: %s' % message['id'])
- return message
- except errors.HttpError as error:
- print('An error occurred: %s' % error)
- return "Error"
- return "OK"
- def SendMessage(sender, to, subject, attachmentFile=None):
- credentials = get_credentials()
- http = credentials.authorize(httplib2.Http())
- service = discovery.build('gmail', 'v1', http=http)
- if attachmentFile:
- message1 = createMessageWithAttachment(sender, to, subject, attachmentFile)
- result = SendMessageInternal(service, "me", message1)
- return result
- def createMessageWithAttachment(
- sender, to, subject, attachmentFile):
- message = MIMEMultipart('mixed')
- messageA = MIMEMultipart('alternative')
- message.attach(messageA)
- print("create_message_with_attachment: file: %s" % attachmentFile)
- content_type, encoding = mimetypes.guess_type(attachmentFile)
- if content_type is None or encoding is not None:
- content_type = 'application/octet-stream'
- main_type, sub_type = content_type.split('/', 1)
- fp = open(attachmentFile, 'rb')
- msg = MIMEBase(main_type, sub_type)
- msg.set_payload(fp.read())
- fp.close()
- encoders.encode_base64(msg)
- filename = os.path.basename(attachmentFile)
- msg.add_header('Content-Disposition', 'attachment', filename=filename)
- msg.add_header('To', to)
- msg.add_header('Subject', subject)
- message.attach(msg)
- raw = base64.urlsafe_b64encode(msg.as_bytes())
- raw = raw.decode()
- return {'raw': raw}
- def main():
- sender = 'toby.lat12@gmail.com'
- subject = 'January Invoice'
- emaillist = ['toby.lat12@gmail.com','theminecraftmastermind686@gmail.com']
- for item in emaillist:
- to = item
- attachpath = 'temp_multi_page_{}.pdf'.format(emaillist.index(item)+1)
- SendMessage(sender, to, subject, attachpath)
- if __name__ == '__main__':
- pdf_splitter('multi.pdf')
- main()
- time.sleep(15)
- pdf_temp_del()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement