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 SMTPConfirmSent()— Just a debugger to see if the email has been saved or sent.— Supposed to show the latest email in the Drafts or Sent folderCONTROLLERdef 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)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)
def ConfirmSent():
importVIEWSComposeMail()ConfirmSent()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
messageSent = imapdb(imapdb.Gmail_Sent_Mail.created == datetime.date.today()).select().first()
— How to extract files (attachments) from an HTML form and append it to an email message in MIME Multipart format?
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