Email | SMTP | Compose Form with Attachments + Send via SMTP

122 views
Skip to first unread message

PRACHI VAKHARIA

unread,
Sep 2, 2013, 1:19:52 PM9/2/13
to web...@googlegroups.com


 
 
Email | SMTP | Compose Form with Attachments + Send via SMTP

 
How to create a
 single C
ompose form that will take all the needed parameters – Including Attachments –​
 ​ ​
and send the Email via SMTP server or Save the Email to IMAP server?

The following code was tried to
 ​ ​
​S
end
 ​ Email using SMTP​
, but how to Send
 ​ Mutliple ​
​Attachments
 that way?

​sendForm = Compose Form
SendSMTP = SMTP send
sendConfirm = Debug Confirmation



def sendForm():
    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, session):
        session.subject = form.vars.subject
        session.body = form.vars.body
        session.to = form.vars.to
        # session.attachment = form.vars.attachment
redirect(URL(f="SendSMTP")) return dict(form=form)

def SendSMTP(): mail.send(
session.to, session.subject, session.body ) session.flash = "Message Sent To : ", session.to redirect(URL(f="sentConfirm")) return dict()

def sentConfirm(): return dict( to = session.to, subject = session.subject, body = session.body, # attachment = session.attachment
)

QUESTIONS
  • How to create a Compose form and Send
    ​ Multiple ​
     ​Attachments
    ?

  • How to use the Same Form to Save as Draft – including the Attachments – on the IMAP server?
  • The above must be done without using any additional backend DB
    ​ — must be done using only IMAP and SMTP​
    .

Thank you, very much.

PRACHI 


 


 

Alan Etkin

unread,
Sep 2, 2013, 3:41:52 PM9/2/13
to
  • How to create a Compose form and Send
    ​ Multiple ​
     ​Attachments
    ?

You create a SQLFORM.factory form and fill the mail.send input with the values collected on form submission. I guess you could use multiple file upload fields in the form factory and show or hide them with jQuery events.

Also check the book on multiple attachments (you should use file objects as input instead of the file paths and IIRC you also need to specify filenames when creating the Attachment objects)
http://www.web2py.com/books/default/chapter/29/08/emails-and-sms#Multiple-attachments
 
  • How to use the Same Form to Save as Draft – including the Attachments – on the IMAP server?
 Almost the same but for the attachments you must use the same format returned to retrieve imap records: a list of dictionaries with this structure:

[{"encoding": ..., "filename": ..., "payload": ..., "mime": }, ...]

where payload contains the raw message part data and mime the part type specificied

EDIT: To save as draft you need the trunk version and use this syntax:

imap.<draft box>.insert(to=..., sender=..., subject=..., attachments=...) # imap is an IMAPAdapter connection
  • The above must be done without using any additional backend DB
    ​ — must be done using only IMAP and SMTP​
    .
I belive the steps detailed above do not actually require a secondary database. However, it is an odd requirement for a web2py appication. What's the rationale for that?

PRACHI VAKHARIA

unread,
Sep 2, 2013, 4:52:50 PM9/2/13
to web...@googlegroups.com



Dear Alan,
Thank you. Please find my questions and attempts below. 



"create a SQLFORM.factory form and fill the mail.send input with the values collected on form submission. I guess you could use multiple file upload fields in the form factory and show or hide them with jQuery events." 
 

Can you kindly post a code snippet to demonstrate that?
 
It is easy to see the code and try to understand it. And even "ReUse" the code, is possible. It is better to learn things right and elegant from someone who knows and understands it well than to learn it partially and broken and inelegantly. So, can you kindly post a code snippet to demonstrate that?

I tried the following with some half luck – but Attachments do NOT work!


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):
mail.send(
form.vars.to,
form.vars.subject,
form.vars.body,
# attachments = mail.attachment(form.vars.attachment),
)
response.flash = "Message Sent To : ", form.vars.to
redirect(URL(f="ConfirmSent"))
return dict(form=form)

def ConfirmSent():
import datetime
message = imapdb(imapdb.Gmail_Sent_Mail.created == datetime.date.today()).select().first()
return dict(message=message) 


How can I get the values directly from the ComposeMail() in ConfirmSent() instead of fetching the mail from IMAP server's sent folder?




"you should use file objects as input instead of the file paths" 
 
How does one do that in a form in the controller of web2py and pass it to the mail.send(...) function?

 


EDIT: To save as draft you need the trunk version and use this syntax:
imap.<draft box>.insert(to=..., sender=..., subject=..., attachments=...) 
# imap is an IMAPAdapter connection

Great! This is wonderful! Yay!


 
"What's the rationale for that?"
 
The aim is to learn IMAP and SMTP systems using Python to recreate and explore the full functionalities of an email system — read, compose, reply, send, delete, attach files, etc.
IMAP, POP, and SMTP are so commonly used by everyone everyday, and to learn its workings as well as to use them will be a great thing to learn and master. 



Thank you very much.
PRACHI





 
 

PRACHI VAKHARIA

unread,
Sep 4, 2013, 12:06:07 PM9/4/13
to web...@googlegroups.com

Someone, please help !

.

Niphlod

unread,
Sep 4, 2013, 2:25:54 PM9/4/13
to web...@googlegroups.com
<offtopic>someone, please hire! ;-)</offtopic>

On Wednesday, September 4, 2013 6:06:07 PM UTC+2, PRACHI VAKHARIA wrote:

Someone, please help !

.

Alan Etkin

unread,
Sep 4, 2013, 6:06:34 PM9/4/13
to web...@googlegroups.com
<offtopic>someone, please hire! ;-)</offtopic>

lol (ontopic in my opinion)

Prachi, have a look at this recipe

www.web2pyslices.com/slice/show/1706/how-to-compose-a-multi-attachment-draftnormal-email-with-imapsmtp

The code is released with AGPLv3 license
Reply all
Reply to author
Forward
0 new messages