Im using the following code to send a message with a .docx attached (all personal emails replaced with @gmail.com)
from __future__ import print_functionimport httplib2import osimport base64from email.mime.audio import MIMEAudiofrom email.mime.base import MIMEBasefrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextimport mimetypesfrom httplib2 import Httpfrom apiclient import errorsfrom datetime import datefrom apiclient.discovery import buildimport httplib2import osfrom apiclient import discoveryimport oauth2clientfrom oauth2client import clientfrom oauth2client import toolsfrom oauth2client import filedef get_credentials():"""Gets valid user credentials from storage. If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials. Returns: Credentials, the obtained credential.""" 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-quickstart.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 if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to '+ credential_path) return credentialscredentials = get_credentials()service = build('gmail', 'v1', http=credentials.authorize(Http()))def send_message(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 Exception as e: print('An error occurred: %s' % e) return Nonedef create_message_with_attachment(sender, to, subject, message_text, file): message = MIMEMultipart() message['to'] = to message['from'] = sender message['subject'] = subject msg = MIMEText(message_text) message.attach(msg) content_type, encoding = mimetypes.guess_type(file) if content_type is None or encoding is not None: content_type = 'application/octet-stream' main_type, sub_type = content_type.split('/', 1) if main_type == 'text': fp = open(file, 'rb') msg = MIMEText(fp.read().decode("utf-8"), _subtype=sub_type) fp.close() elif main_type == 'image': fp = open(file, 'rb') msg = MIMEImage(fp.read(), _subtype=sub_type) fp.close() elif main_type == 'audio': fp = open(file, 'rb') msg = MIMEAudio(fp.read(), _subtype=sub_type) fp.close() else: fp = open(file, 'rb') msg = MIMEBase(main_type, sub_type) msg.set_payload(fp.read()) fp.close() filename = os.path.basename(file) msg.add_header('Content-Disposition', 'attachment', filename=filename) message.attach(msg) raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8")) return {'raw': raw_message.decode("utf-8")}today = date.today()dte = today.strftime("%d.%m.%Y")teacher = ["Hugo Bohácsek","Jana Manczálová","Iveta Rybárová"]adr = ["@gmail.com","@gmail.com","@gmail.com"]sub_a = ["Informatika","Fyzika","Matematika"]sub = ["Informatiky","Fyziky","Matematiky"]r = 0for i in teacher: r+=1 print(r,") ",i, sep="")who = int(input("Číslo vybraného učiteľa: "))-1file_name = "%s_uloha.docx" %sub_a[who].lower()a = "Riešenie príkladov z %s" %sub[who]b = "Dobrý deň,\nPosielam vám vypracovanéúlohy z %s.\nHugo Bohácsek, Ki.B %s" %(sub[who],dte)mine = create_message_with_attachment("@gmail.com",adr[who],a,b,file_name)testSend = send_message(service, 'me', mine)
The problem:I've read that the corruption is caused by Null being inserted into the document, by replacing it with "" the problem is fixed. Any idea how to do that? Also when i try to change the attachment name, word wont open the file anymore.