EMAIL | IMAP + SMTP | Compose Email = Save as Draft + Send via SMTP

403 views
Skip to first unread message

PRACHI VAKHARIA

unread,
Oct 13, 2013, 12:29:39 PM10/13/13
to web...@googlegroups.com



 
EMAIL | IMAP + SMTP
 
Compose Email | Save as Draft + Send via SMTP

 
Please look at the code below and give me some feedback on how to improve it.

ComposeMail()
— Creates a form with Email fields
— If form accepted » Save as Draft in IMAP | Send via SMTP
ConfirmSent()
— Just a debugger to see if the email has been saved or sent.
— Supposed to show the latest email in the Drafts or Sent folder


CONTROLLER

def ComposeMail():
form = SQLFORM.factory( Field("subject", requires=IS_NOT_EMPTY()), Field("body", "text", requires=IS_NOT_EMPTY()), Field("to", requires=IS_NOT_EMPTY()), Field("attachment", "upload") )
    if form.accepts(request.vars):
        draft_id = imapdb.Gmail_Drafts.insert(to=form.vars.to, subject=form.vars.subject, content=form.vars.body, draft=True)
        mail.send(
form.vars.to, form.vars.subject,
form.vars.body, # attachments = mail.attachment(form.vars.attachment),
)
        redirect(URL(f="ConfirmSent"))
return dict(form=form)


def ConfirmSent():
import
datetime messageSent = imapdb(imapdb.Gmail_Sent_Mail.created == datetime.date.today()).select().first() messageSaved = imapdb(imapdb.Gmail_Drafts.created == datetime.date.today()).select().first() return dict(Sent=messageSent, Saved=messageSaved)



VIEW​S

​ComposeMail()

{{=BEAUTIFY(response._vars)}}


ConfirmSent()​

{{=BEAUTIFY(response._vars)}}







Questions
— How to extract files (attachments) from an HTML form and append it to an email message in MIME Multipart format?
— How to modify the code above for the same?


 
— PRACHI





 
 


 

Alan Etkin

unread,
Oct 13, 2013, 2:23:32 PM10/13/13
to web...@googlegroups.com
messageSent = imapdb(imapdb.Gmail_Sent_Mail.created == datetime.date.today()).select().first()
 
If you want to check that the message was stored at the imap service folder, it would be better to save subject and sender to the session for smaller query results, your query is retrieving any message stored in the sent folder.

# (in compose action)
>>> session.subject, session.sender = form.vars.subject, form.vars.sender

# then
>>> query = imapdb.Gmail_Sent_Mail.created == request.now.date()
>>> query &= imapdb.Gmail_Sent_Mail.sender.contains(session.sender)
>>> query &= imapdb.Gmail_Sent_Mail.subject.contains(session.subject)
>>> sent = imapdb(query).select().first()Questions

— How to extract files (attachments) from an HTML form and append it to an email message in MIME Multipart format?

PRACHI VAKHARIA

unread,
Oct 22, 2014, 1:01:06 AM10/22/14
to web...@googlegroups.com, Alan Etkin, + SHABIN RAJ, Massimo Di Pierro, ♕ PRACHI VAKHARIA



 
Dear Alan and Massimo,

First, please look at this site:

It shows how to upload and read multiple files directly using HTML5 (and JavaScript).
It uses the File API specification from W3:


How to implement that in Web2Py for Email Composing with File Attachments?


Controller

def Composer():
form = FORM(TABLE(
TR('Subject:', INPUT(_type='text', _name='subject',
requires=IS_NOT_EMPTY())),
TR('Email To:', INPUT(_type='text', _name='emailto', _multiple="",
requires=IS_EMAIL())),
TR('Save Draft?', SELECT('yes', 'no', _name='savedraft',
requires=IS_IN_SET(['yes', 'no']))),
TR('Body', TEXTAREA(_name='body',
value='Body of email')),
TR( 'File', INPUT(_type='file', _id="files", _name="files[]", _multiple="", _value='File test') ),
TR('', INPUT(_type='submit', _value='EMAIL'))
))

if form.process().accepted:
response.flash = 'Form accepted'

if form.vars.savedraft:
## SAVE DRAFT on IMAP ##
draft_id = imapdb.Gmail_Drafts.insert(
to=form.vars.emailto, subject=form.vars.subject, content=form.vars.body, draft=True,
attachments = [ mail.Attachment( form.vars.FILE.file.read(), filename=form.vars.FILE.filename ) ]
)
response.flash = 'Email Saved'
else:
## SEND Email by SMTP ##
mail.send(
to = form.vars.emailto,
subject = form.vars.subject,
message = (form.vars.body, '<html>' + form.vars.body + '</html>'),
attachments = [ mail.Attachment( form.vars.FILE.file.read(), filename=form.vars.FILE.filename ) ]
)
response.flash = 'Email Sent'

elif form.errors:
response.flash = 'Errors in form'
else:
response.flash = 'Compose Email'
return dict(form=form) 




Questions
  • How to use   <input type='file', name="files[]", multiple="">   to input files for Attachment to email?
  • How to extract the required file attributes and append them directly into the mail message and attachment?
  • If the HTML5 provides a means to input, read and upload files, why cannot we use that directly in web2py Email to read and attach multiple files?
 
Please look into this and help implementing this.


Thank you, very much.

Gratefully, 
PRACHI V






_


Massimo Di Pierro

unread,
Oct 23, 2014, 10:16:46 AM10/23/14
to web...@googlegroups.com, spam...@gmail.com, ShabinT...@gmail.com, massimo....@gmail.com, Prachi....@gmail.com
This should help:

def index():
    form = FORM(INPUT(_type='file', _id="files", _name="files", _multiple=True, _value='File test'),
                INPUT(_type="submit")).process()
    if form.accepted:
        for item in form.vars.files:
            print item.filename, item.file.read()
    return dict(form=form)

Tim Richardson

unread,
Oct 25, 2014, 4:32:36 AM10/25/14
to web...@googlegroups.com, spam...@gmail.com, ShabinT...@gmail.com, massimo....@gmail.com, Prachi....@gmail.com
I haven't had a good experience with mail.send
It seems to block python which is a problem if you have a single process webserver (like Rocket, or apache on windows) . 
You should probably use the scheduler for sending mail (since this means the process which gets blocked is not the webserver). 

gmail has a REST API which I assume is non blocking; I'm going to learn how to use that in the next couple of weeks for a client which has migrated to Google Apps. This is I hope a good, reusable solution for sending single emails without needing a mail processing queue via the scheduler. 

Massimo Di Pierro

unread,
Oct 25, 2014, 12:12:45 PM10/25/14
to web...@googlegroups.com, spam...@gmail.com, ShabinT...@gmail.com, massimo....@gmail.com, Prachi....@gmail.com
Yes it blocks. It is better to use the scheduler to send emails in background.
Reply all
Reply to author
Forward
0 new messages