orveo Newbie

Joined: 27 Mar 2010 Posts: 1
|
Posted: Thu Sep 02, 2010 12:39 pm Post subject: (python)correct syntax of file for sending mail |
|
|
hello
i build à little program to send different mail to selected people.
i made a dialog which call 2 python function.
the first has the parameter inside the PY function and works well
the second get data in the sheet of the calc document
to run then sendmail function ,i need 6 parameters
send from,send to,title,bodytext,smtpserver name and file for the attachment.
for the first 5 element no problem it works well
my problem is with the file location of the attachment.
when the program arrive to this line :
part.set_payload( open(files,"rb").read() )
it stops with message "no file or no directory" in french
the parameters in the spreadsheet are the same than in the getdataInside function
could you help me to find the correct syntax .it is in the getdataoutside function
see the remarks
excuse me for my english but it is not my mother tongue
i am looking for a solution for 3 days so coulld you help me
thanks
| Code: |
import smtplib
import os
from com.sun.star.awt import Rectangle
from com.sun.star.awt import WindowDescriptor
from com.sun.star.awt.WindowClass import MODALTOP
from com.sun.star.awt.VclWindowPeerAttribute import OK, OK_CANCEL, YES_NO, YES_NO_CANCEL, RETRY_CANCEL, DEF_OK, DEF_CANCEL, DEF_RETRY, DEF_YES, DEF_NO
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def getdataInside():
send_from="myadress@gmail.com" # CHANGE WITH A VALID ADRESS
send_to="myadress@gmail.com" # CHANGE WITH A VALID ADRESS
subject=" confirmation d'inscription à notre club" # message title DOESN'T NEED TO CHANGE FOR TEST
files='/home/USERNAME/.openoffice.org/3/user/Scripts/python/msg1.txt' # way to the attachment in a linux system file
# PUT msg1.txt IN YOUR USER NAME DIRECTORY write the same adress as in the sheet"adherent"
text="""bonjour
Nous sommes heureux de vous compter parmi nos adherents.
Vous pourrez d'ici la fin du mois beneficier de nos instalation sportiveS
Et profitez de l'ageable ambiance qui regne dans notre club.
Cordialement
Le bureau
""" # body of email utilisation des triples """ pour la variable texte sur plusieurs lignes
server="smtp.free.fr" # CHANGE WITH A VALID SERVER
send_mailInside(send_from, send_to, subject, text, files, server) # call mailInside function
def send_mailInside(send_from, send_to, subject, text, files, server):
# works fine
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
part = MIMEBase('application', "octet-stream")
part.set_payload( open(files,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(files))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
def getdataOutside():
# GET DATA FROM SHEET "adherent"
document = XSCRIPTCONTEXT.getDocument()
feuilles = document.Sheets
feuilledata=feuilles.getByName("adherent")
cellule=feuilledata.getCellByPosition (6,1)
send_from = cellule.String
cellule2=feuilledata.getCellByPosition (6,2)
send_to=cellule2.String
cellule3=feuilledata.getCellByPosition (6,3)
subject=cellule3.String
cellule4=feuilledata.getCellByPosition (6,4)
text=cellule4.String
cellule5=feuilledata.getCellByPosition (6,5)
files= "'" + cellule5.String + "'" # ///////THE PROBLEM IS HERE.//////
# HELP ME TO FIND THE CORRECT SYNTAX FOR THE FILE ADRESS
TestMessageBox(files)
cellule6=feuilledata.getCellByPosition (6,6)
server=cellule6.String
cellule7=feuilledata.getCellByPosition( 6,15 ) # back up to check this parameter .just for test
cellule7.setString( send_from)
cellule8=feuilledata.getCellByPosition( 6,16 ) # back up to check this parameter .just for test
cellule8.setString( files)
send_mailOutside(send_from, send_to, subject, text, files, server) #call mailOutside function
def send_mailOutside(send_from, send_to, subject, text, files, server):
TestMessageBox(send_from) #msgbox to check each parameter FOR TEST ONLY
TestMessageBox(send_to)
TestMessageBox(subject)
TestMessageBox(files) # SEEMS TO BE GOOD
TestMessageBox(text)
TestMessageBox(server)
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
part = MIMEBase('application', "octet-stream")
#part.set_payload( open(files,"rb").read() ) # THE PROBLEME IS HERE .IT DOESN'T FIND THE FILE
#LIKE THAT, email is sent without attachment
# if you remove "#" the program stops
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(files))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
def TestMessageBox(mes):
doc = XSCRIPTCONTEXT.getDocument()
parentwin = doc.CurrentController.Frame.ContainerWindow
s = mes
t = "Test"
res = MessageBox(parentwin, s, t, "querybox", YES_NO_CANCEL + DEF_NO)
#s = res
#MessageBox(parentwin, s, t, "infobox")
# Show a message box with the UNO based toolkit
def MessageBox(ParentWin, MsgText, MsgTitle, MsgType="messbox", MsgButtons=OK):
MsgType = MsgType.lower()
#available msg types
MsgTypes = ("messbox", "infobox", "errorbox", "warningbox", "querybox")
if not ( MsgType in MsgTypes ):
MsgType = "messbox"
#describe window properties.
aDescriptor = WindowDescriptor()
aDescriptor.Type = MODALTOP
aDescriptor.WindowServiceName = MsgType
aDescriptor.ParentIndex = -1
aDescriptor.Parent = ParentWin
#aDescriptor.Bounds = Rectangle()
aDescriptor.WindowAttributes = MsgButtons
tk = ParentWin.getToolkit()
msgbox = tk.createWindow(aDescriptor)
msgbox.setMessageText(MsgText)
if MsgTitle :
msgbox.setCaptionText(MsgTitle)
return msgbox.execute()
|
|
|