2011-10-27 15 views
5

पर एक फ़ाइल को संलग्न करना कृपया मुझे आगे माफ कर दें। जब मैंने इस प्रश्न का शोध करने की कोशिश की है तो मैं उस कोड को देख रहा हूं जिसे मैं समझ नहीं पा रहा हूं। मेरे पास पाइथन के साथ लगभग 3 घंटे का अनुभव है और शायद मैं इसे संभालने से अधिक प्रयास कर रहा हूं।ई-मेल

समस्या सरल है। मैं ई-मेल भेजने के लिए सफलतापूर्वक आर (मेरे विश्लेषण सॉफ्टवेयर) से पायथन को कॉल कर सकता हूं। संदेश, विषय, से, और फ़ील्ड से जोड़ना मैं कर सकता हूं। मैं एक अनुलग्नक भेजने में सक्षम होना चाहता हूँ। अगर मैं सिर्फ एक लगाव भेज सकता तो जीवन बहुत अच्छा होगा। [प्रकार 'सूची']

(लेकिन < नहीं [के साथ):

कोड मेरे पास अब तक

import smtplib 
import os 
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 email.utils 

fromaddr = '[email protected]' 
toaddrs = '[email protected]' 
msg = MIMEMultipart(MIMEText('This is the body of the e-mail')) 
msg['From'] = email.utils.formataddr(('Benjamin Nutter', fromaddr)) 
msg['To'] = email.utils.formataddr(('Benjamin Nutter', toaddrs)) 
msg['Subject'] = 'Simple test message' 
f = 'filename.pdf' 
part=MIMEBase('application', 'octet-stream') 
part.set_payload(open(f, 'rb').read()) 
Encoders.encode_base64(part) 
part.add_header('Content-Disposition', 'attachment; filename=\"%s\"' % os.path.basename(f)) 
msg.attach(part) 

"username = 'user' 
"password = 'pw' 

server = smtplib.SMTP('smtp.gmail.com:587') 
server.ehlo() 
server.starttls() 
server.ehlo() 
server.login(username,password) 
server.sendmail(fromaddr, toaddrs, msg.as_string()) 
server.quit() 

मुझे इस कोड चलाने के लिए, मैं संदेश स्ट्रिंग पेलोड की उम्मीद मिलता है मैं आज आत्म-शिक्षा के लिए अपनी सीमा पर हूं। मुझे उम्मीद है कि यह किसी से अधिक अनुभवी व्यक्ति के लिए एक स्पष्ट फिक्स है।

मुझे आशा है कि आप सभी का दिन अच्छा रहेगा।

+0

मैं बहुत अजगर पता नहीं है, लेकिन yourr की "[" केवल उपयोग लाइनों में है के बाद से "msg [से 'के साथ शुरू," msg [करने के लिए "और" msg [विषय "है कि यह सुनिश्चित करें की तरह लगता है <] को बदलने के लिए जगह <>। –

+1

इसी तरह का कोड मेरे लिए काम करता प्रतीत होता है। क्या आप पूर्ण ट्रेसबैक पोस्ट कर सकते हैं? इसके अलावा, ऐसा लगता है कि आर के साथ कुछ भी नहीं है, इसलिए मैं उस टैग को हटाने जा रहा हूं। –

+0

आप सही दिखते हैं। जब मैं आज सुबह आया और भाग गया, तो यह ठीक काम करता था। मैंने नीचे एक आर समारोह में अनुकूलन पोस्ट किया। इसे देखने के लिए धन्यवाद। – Benjamin

उत्तर

0

मुझे पता है कि यह मेरे स्वयं के प्रश्न का उत्तर देने के लिए यह बुरा फॉर्म है, लेकिन यह बिना किसी बदलाव के चमत्कारिक रूप से काम करना शुरू कर दिया। मेरा पहला प्रभाव बनाने का क्या तरीका है, है ना?

वैसे भी, मैंने इसे एक आर फ़ंक्शन में लपेट लिया। यह जीमेल से भेजेगा, लेकिन मैंने इसे अभी तक अन्य खातों से भेजने की कोशिश नहीं की है। मुझे Outlook से भेजने में सबसे अधिक दिलचस्पी है, क्योंकि मैं इसका उपयोग अपनी स्क्रिप्ट के भीतर से विश्लेषण रिपोर्ट भेजने के लिए कर रहा हूं। जब मैंने अपने नियोक्ता के एसएमटीपी सर्वर में प्रवेश किया, तो उसने त्रुटि "एसएमटीपी AUTH एक्सटेंशन सर्वर द्वारा समर्थित नहीं है।" मुझे संदेह है कि मुझे अपने तकनीकी सहायता लोगों के साथ यह काम करना होगा।

यह शायद WinDialog() फ़ंक्शंस के लिए धन्यवाद, विंडोज पर ही काम करेगा। लेकिन यह एक अच्छी शुरुआत है।

send.email <- function(to, from, subject, 
    message, attachment=NULL, 
    username, password, 
    server="smtp.gmail.com:587", 
    confirmBeforeSend=TRUE){ 
    # to: a list object of length 1. Using list("Recipient" = "[email protected]") will send the message to the address but 
    #  the name will appear instead of the address. 
    # from: a list object of length 1. Same behavior as 'to' 
    # subject: Character(1) giving the subject line. 
    # message: Character(1) giving the body of the message 
    # attachment: Character(1) giving the location of the attachment 
    # username: character(1) giving the username. If missing and you are using Windows, R will prompt you for the username. 
    # password: character(1) giving the password. If missing and you are using Windows, R will prompt you for the password. 
    # server: character(1) giving the smtp server. 
    # confirmBeforeSend: Logical. If True, a dialog box appears seeking confirmation before sending the e-mail. This is to 
    #     prevent me to send multiple updates to a collaborator while I am working interactively. 

    if (!is.list(to) | !is.list(from)) stop("'to' and 'from' must be lists") 
    if (length(from) > 1) stop("'from' must have length 1") 
    if (length(to) > 1) stop("'send.email' currently only supports one recipient e-mail address") 
    if (length(attachment) > 1) stop("'send.email' can currently send only one attachment") 
    if (length(message) > 1){ 
    stop("'message' must be of length 1") 
    message <- paste(message, collapse="\\n\\n") 
    } 

    if (is.null(names(to))) names(to) <- to 
    if (is.null(names(from))) names(from) <- from 
    if (!is.null(attachment)) if (!file.exists(attachment)) stop(paste("'", attachment, "' does not exist!", sep="")) 

    if (missing(username)) username <- winDialogString("Please enter your e-mail username", "") 
    if (missing(password)) password <- winDialogString("Please enter your e-mail password", "") 

    require(rJython) 
    rJython <- rJython() 

    rJython$exec("import smtplib") 
    rJython$exec("import os") 
    rJython$exec("from email.MIMEMultipart import MIMEMultipart") 
    rJython$exec("from email.MIMEBase import MIMEBase") 
    rJython$exec("from email.MIMEText import MIMEText") 
    rJython$exec("from email.Utils import COMMASPACE, formatdate") 
    rJython$exec("from email import Encoders") 
    rJython$exec("import email.utils") 

    mail<-c(
    #Email settings 
    paste("fromaddr = '", from, "'", sep=""), 
    paste("toaddrs = '", to, "'", sep=""), 
    "msg = MIMEMultipart()", 
    paste("msg.attach(MIMEText('", message, "'))", sep=""), 
    paste("msg['From'] = email.utils.formataddr(('", names(from), "', fromaddr))", sep=""), 
    paste("msg['To'] = email.utils.formataddr(('", names(to), "', toaddrs))", sep=""), 
    paste("msg['Subject'] = '", subject, "'", sep="")) 

    if (!is.null(attachment)){ 
    mail <- c(mail, 
     paste("f = '", attachment, "'", sep=""), 
    "part=MIMEBase('application', 'octet-stream')", 
    "part.set_payload(open(f, 'rb').read())", 
    "Encoders.encode_base64(part)", 
    "part.add_header('Content-Disposition', 'attachment; filename=\"%s\"' % os.path.basename(f))", 
    "msg.attach(part)") 
    } 

#SMTP server credentials 
    mail <- c(mail, 
    paste("username = '", username, "'", sep=""), 
    paste("password = '", password, "'", sep=""), 

#Set SMTP server and send email, e.g., google mail SMTP server 
    paste("server = smtplib.SMTP('", server, "')", sep=""), 
    "server.ehlo()", 
    "server.starttls()", 
    "server.ehlo()", 
    "server.login(username,password)", 
    "server.sendmail(fromaddr, toaddrs, msg.as_string())", 
    "server.quit()") 

    message.details <- 
    paste("To:    ", names(to), " (", unlist(to), ")", "\n", 
      "From:    ", names(from), " (", unlist(from), ")", "\n", 
      "Using server:  ", server, "\n", 
      "Subject:   ", subject, "\n", 
      "With Attachments: ", attachment, "\n", 
      "And the message:\n", message, "\n", sep="") 

    if (confirmBeforeSend) 
    SEND <- winDialog("yesnocancel", paste("Are you sure you want to send this e-mail to ", unlist(to), "?", sep="")) 
    else SEND <- "YES" 

    if (SEND %in% "YES"){ 
    jython.exec(rJython,mail) 
    cat(message.details) 
    } 
    else cat("E-mail Delivery was Canceled by the User") 
} 
+1

अपने खुद के प्रश्न का उत्तर देने के लिए कोई बुरा फॉर्म नहीं है - यह भी प्रोत्साहित किया जाता है! http://meta.stackexchange.com/questions/9933/is-there-a-convention-for-accepting-my-own-answer-to-my-own-question – Chadwick

1

आप सीधे SMTP का उपयोग करने की कोशिश करने के बजाय 'मेलर' का उपयोग करने का प्रयास कर सकते हैं। मेलर here पाया जा सकता है।

यहां कुछ सरल कोड है जो दिखाता है कि यह कैसे काम करता है।

messages=[] 
message = mailer.Message() 
message.attach('filename.txt') 
message.From = 'Cool guy <[email protected]>' 
message.To = 'Random Dude <[email protected]>' 
message.Cc = 'Cards Fan <[email protected]>' 
message.Subject = 'Test Email' 
message.body = 'Here is the body of the email.' 
messages.append(message) 

emailer = mailer.Mailer(smtphost.example.com) 
emailer.send(messages) 

मैंने इसे स्थानीय रूप से कुछ उदाहरणों से एक साथ जोड़ दिया। उपरोक्त लिंकर पेज भी अन्य उदाहरण दिखाता है। एक बार मुझे यह कोड मिला, मैंने इस पैकेज का उपयोग करने के लिए अपने सभी अन्य पायथन ईमेल कोड को परिवर्तित कर दिया।

+0

मैंने कुछ काम करने से पहले इसे आजमाया। दुर्भाग्य से, बिना पाइथन वितरण के और मेरे कोड को आर के माध्यम से चलाते हुए, यह पैकेज को पहचानने के लिए प्रतीत नहीं होता था और मुझे नहीं पता था कि काम कैसे प्राप्त किया जाए। क्षमा करें मैं एक और अधिक उत्पादक छात्र नहीं हो सकता है। – Benjamin