Home Download Buy Blog Forum Support

is there a SublimeGmail Plugin?

Re: is there a SublimeGmail Plugin?

Postby jbjornson on Fri Nov 29, 2013 3:06 pm

aim9x wrote:I find this plugin very useful. I'd like to be able to add a input panel that allows you to customize/enter the subject line. How should I go about doing that?


Try this (a small change to the gmail.py code from here: https://github.com/Skarlso/SublimeGmailPlugin):

Code: Select all
import sublime, sublime_plugin, smtplib

class GmailCommand(sublime_plugin.TextCommand):
    to = "example@gmail.com"
    text = "selected text"

    def on_done(self, to, text):
        self.view.window().show_input_panel("Subject:", 'Sent from SublimeText', lambda s, content=text, recipient=to: self.send_gmail(recipient, content, s), None, None)


    def send_gmail(self, to, text, subject):
        gmail_user = "your@gmail.com"
        gmail_pwd = "yourpassword"
        FROM = 'your@gmail.com'
        TO = ['%s' % to] # from input
        SUBJECT = subject
        TEXT = text

        message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
            """ % (FROM, ", ".join(TO), SUBJECT, TEXT)

        try:
            #server = smtplib.SMTP(SERVER)
            server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
            server.ehlo()
            server.starttls()
            server.login(gmail_user, gmail_pwd)
            server.sendmail(FROM, TO, message)
            #server.quit()
            server.close()
            sublime.status_message("Email sent successfully to: %s" % to)
        except:
            sublime.status_message("There was an error sending the email to: %s " % to)


    def run(self, edit):
        for region in self.view.sel():
            if not region.empty():
                # Get the selected text
                text = self.view.substr(region)
                self.view.window().show_input_panel("To:", 'email@gmail.com', lambda s, content=text: self.on_done(s, content), None, None)


Note: I cannot test this as smtp access is blocked at work but I only changed the on_done function and the signature and subject assignment in the send_gmail function (so it would work as well as it did before).
jbjornson
 
Posts: 335
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: is there a SublimeGmail Plugin?

Postby aim9x on Fri Nov 29, 2013 4:35 pm

Thanks for the reply! No, it doesn't seem to work.
I figured I needed to change the on_done function. I'm more used to vanilla C programming for microcontrollers and object-oriented programming seems a bit confusing for me ... Especially the lambda function and s variable as it doesn't seem to be defined.

How exactly does
Code: Select all
self.view.window().show_input_panel("Subject:", 'Sent from SublimeText', lambda s, content=text, recipient=to: self.send_gmail(recipient, content, s), None, None)

get the subject string so that it can be attributed to the SUBJECT variable?
aim9x
 
Posts: 3
Joined: Fri Nov 29, 2013 1:15 pm

Re: is there a SublimeGmail Plugin?

Postby aim9x on Fri Nov 29, 2013 7:33 pm

Yeeeah... Sorry... It actually DOES work.
I just kept forgetting to actually input my username/password
Sorry...
aim9x
 
Posts: 3
Joined: Fri Nov 29, 2013 1:15 pm

Re: is there a SublimeGmail Plugin?

Postby jbjornson on Mon Dec 02, 2013 8:40 am

Cool. Glad to help.
jbjornson
 
Posts: 335
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Previous

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests