2015-02-17 176 views
0

當我concatentate一個字符串變量會議邀請芯片附件沒有得到正確創建的UID來設置IC的UID,但是當我硬編碼整個字符串無法通過連接字符串變量

"UID: hardcoded string" 

這將創建會議邀請ics附件正確。我不知道如何解決這個問題。我不想使用getuniqueid()函數,因爲我希望能夠取消會議邀請請求。

import smtplib 
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 
import os, datetime 

def meeting_invitation(toAddr, body, datetimeStrP, method, uid): 
    CRLF = "\r\n" 
    attendees = toAddr 
    organizer = "ORGANIZER;CN=organiser:mailto:do.not.reply"+CRLF+" @engr.orst.edu" 
    fro = "<[email protected]>" 

    ddtstart = datetimeStrP 
    dtoff = datetime.timedelta(hours = 8) # Correct -8 hour UTC offset correction 
    dur = datetime.timedelta(minutes = 15) 
    ddtstart = ddtstart + dtoff 
    dtend = ddtstart + dur 
    dtstamp = datetime.datetime.now().strftime("%Y%m%dT%H%M%SZ") 
    dtstart = ddtstart.strftime("%Y%m%dT%H%M%SZ") 
    dtend = dtend.strftime("%Y%m%dT%H%M%SZ") 

    if method == "REQUEST": 
     status = "CONFIRMED" 
    elif method == "CANCEL": 
     status = "CANCELLED" 

    description = "DESCRIPTION: Meeting invitation "+CRLF 
    attendee = "" 
    for att in attendees: 
     attendee += "ATTENDEE;CUTYPE=INDIVIDUAL;" \ 
        "ROLE=REQ-PARTICIPANT;" \ 
        "PARTSTAT=ACCEPTED;" \ 
        "RSVP=TRUE"+CRLF+" ;" \ 
        "CN="+att+";" \ 
        "X-NUM-GUESTS=0:"+CRLF+" " \ 
        "mailto:"+att+CRLF 
    ical = "BEGIN:VCALENDAR"+CRLF+\ 
      "PRODID:pyICSParser"+CRLF+\ 
      "VERSION:2.0"+CRLF+\ 
      "CALSCALE:GREGORIAN"+CRLF 
    ical+="METHOD:"+method+CRLF+\ 
      "BEGIN:VEVENT"+CRLF+\ 
      "DTSTART:"+dtstart+CRLF+\ 
      "DTEND:"+dtend+CRLF+\ 
      "DTSTAMP:"+dtstamp+CRLF+organizer+CRLF 
    ical+= "UID:"+uid+CRLF 
    ical+= "UID:%s" %(uid)+CRLF 
    # ical['uid']=uid+CRLF 
    ical+= attendee+\ 
      "CREATED:"+dtstamp+CRLF+\ 
      description+\ 
      "LAST-MODIFIED:"+dtstamp+CRLF+\ 
      "LOCATION:"+CRLF+\ 
      "SEQUENCE:0"+CRLF+\ 
      "STATUS:"+status+CRLF 
    ical+= "SUMMARY: Meeting invitation "+ddtstart.strftime("%Y%m%d @ %H:%M")+CRLF+\ 
      "TRANSP:OPAQUE"+CRLF+\ 
      "END:VEVENT"+CRLF+\ 
      "END:VCALENDAR"+CRLF 

    eml_body = body 
    msg = MIMEMultipart('mixed') 
    msg['Reply-To']=fro 
    msg['Date'] = formatdate(localtime=True) 
    msg['Subject'] = "Advising Meeting "+ status #+ dtstart 
    msg['From'] = fro 
    msg['To'] = ",".join(attendees) 

    part_email = MIMEText(eml_body, "plain") 
    part_cal = MIMEText(ical,'calendar;method='+method) 

    msgAlternative = MIMEMultipart('alternative') 
    msg.attach(msgAlternative) 

    ical_atch = MIMEBase('application/ics',' ;name="%s"'%("invite.ics")) 
    ical_atch.set_payload(ical) 
    Encoders.encode_base64(ical_atch) 
    ical_atch.add_header('Content-Disposition', 'attachment; filename="%s"'%("invite.ics")) 

    eml_atch = MIMEBase('text/plain','') 
    Encoders.encode_base64(eml_atch) 
    eml_atch.add_header('Content-Transfer-Encoding', "") 

    msgAlternative.attach(part_email) 
    msgAlternative.attach(part_cal) 

    mailServer = smtplib.SMTP('mail.server', 587) 
    mailServer.sendmail(fro, attendees, msg.as_string()) 
    mailServer.close() 
+0

你確定你的uid變量實際上是一個字符串嗎?檢查你可以使用 'if isinstance(uid,str):' – oberron 2015-02-26 11:52:44

+0

就是這樣。發現這是我測試時的Gmail問題,但它適用於Outlook,它實際上會用於發送會議邀請。 – imparante 2015-02-27 12:50:27

回答

0

發現這是一個Gmail的問題時,我的測試,但它的工作原理上的Outlook,它實際上將被用來發送會議邀請。