Email | SMTP | Compose Form with Attachments + Send via SMTP
How to create asingle Compose 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 Send Email using SMTP, but how to Send Mutliple Attachmentsthat way?sendForm = Compose FormSendSMTP = SMTP sendsendConfirm = Debug Confirmationdef 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
- 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.
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
.
Someone, please help !.
<offtopic>someone, please hire! ;-)</offtopic>