To email a backup copy of the currently opened document. I've written a simple OpenOffice.org Basic macro that executes the following Python script:
Code: Select all
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
import sys
GMAIL_USER = 'username@gmail.com'
GMAIL_PASSWD = "password"
RECIPIENT = "backup@gmail.com"
SUBJECT = "Writer's Tools document backup: " + sys.argv[2]
MESSAGE = "A backup version of an OpenOffice.org document is attached."
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = GMAIL_USER
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(GMAIL_USER, GMAIL_PASSWD)
mailServer.sendmail(GMAIL_USER, to, msg.as_string())
mailServer.close()
mail(RECIPIENT, SUBJECT, MESSAGE, sys.argv[1])
Code: Select all
Sub EmailBackup()
Dim Args(0) As New com.sun.star.beans.PropertyValue
ThisDoc=ThisComponent
If ThisDoc.hasLocation=False Then
MsgBox (MsgSaveDocument, 16, MsgAttn) :End
End If
ThisDocURL=ThisDoc.getURL()
If ThisDoc.isModified Then
ThisDoc.storeAsURL(ThisDocURL, Args)
End if
ThisDocURL=ThisDoc.getURL()
PathSubstService = CreateUnoService("com.sun.star.util.PathSubstitution")
HomeDir = ConvertFromURL(PathSubstService.substituteVariables("$(home)", true))
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
FileName=Dir(ThisDocURL, 0)
If GetGuiType()=1 Then
DocPath=ConvertFromURL(ThisDocURL)
Shell("C:\Python25\python.exe", 1, "C:\Python25\gmail_backup.py " & """" & DocPath & """" & """" & FileName & """")
Else
DocDir=DirectoryNameoutofPath(ThisDocURL, "/")
DocPath=ConvertFromURL(DocDir)
Shell("python", 1, HomeDir & "/gmail_backup.py " & DocPath & "/" & FileName & " " & FileName)
End If
End Sub