0

I need to send mail to particular user from the database. i have data table where 1000 of records are available. each record have some email ids. or some id has assigned to multiple record . i need to send record to the particular email only.

I have form from where i used to select company name and strt date. from these i fetch all the value into list. now worries is how to send record to particular user

let my code example

def sendmail(): 
    form = SQLFORM.factory(Field('select_company', db.company, represent=lambda c, row:get_company_name(c), requires = IS_IN_DB(db,'company.id','%(company)s')),
                                    Field('choose_start_date','date',requires=IS_NOT_EMPTY(error_message='Please choose Start Date')),
                                    Field('choose_end_date','date',requires=IS_NOT_EMPTY(error_message='Please choose end Date'))) 
    if form.accepts(request,session):
        session.company = form.vars.select_company
        session.strtdate = form.vars.choose_start_date
        session.enddate = form.vars.choose_end_date
        mailidr() 
    return dict(form=form)


def mailidr():
    from gluon.tools import Mail,datetime
    mail=Mail()
        #specify server
    mail=auth.settings.mailer
    mail.settings.server='smtp.gmail.com:587'
    mail.settings.login='comdummy09@gmail.com:critical@009'
    #specify address to send as
    mail.settings.sender='comdummy09@gmail.com'
    #send the message
    list = []
    for row in db((db.tracker.company == session.company) & (db.tracker.Complience_Status != 1) & (db.tracker.periodicity_due_date >= session.strtdate) & (db.tracker.periodicity_due_date <= session.enddate)).select():
        list.append('<tr><td style="padding-top:5px;padding-bottom:5px;padding-right:5px;padding-left:5px;">'+row.Compliance_Area+'</td><td style="padding-top:5px;padding-bottom:5px;padding-right:5px;padding-left:5px;">'+row.law_description+'</td><td style="padding-top:5px;padding-bottom:5px;padding-right:5px;padding-left:5px;" >''<a href="http://jmdlcmplus.fluxflex.com/jmdlcmplus/default/create" target="_blank"  ">'+row.activity_name+'</a>''</td><td style="padding-top:5px;padding-bottom:5px;padding-right:5px;padding-left:5px;">'+str(row.periodicity_due_date)+'</td></tr>')
    mail.send(to=[row.client_owner],
    subject='Compliance and Due_Date',
    message='<html>'
        '<body>'
                            '<h1>Test Mails</h1>'
                            '<h2>Dear \t' +session.company+ '</h2>'
                            '</br><h3>REMINDER</h3>'
                            '<p>Please note that the fol1ing records are due between &nbsp;' +session.strtdate+ '&nbsp;to&nbsp;' +session.enddate+ '</p>'
                            '<table border=1>'
                                '<thead >'
                                    '<tr>'
                                        '<th style="padding-top:5px;padding-bottom:5px;padding-right:5px;padding-left:5px;">Compliance Area</th>'
                                        '<th>record</th><th>date (Due Date)</th>'
                                    '</tr>'
                                '</thead>'
                                '<tbody>'
                                        +str(list)+
                                '</tbody>'
                            '</table>'
                        '</body>'
                    '</html>')
    response.flash='mail send'
    return ''

from this code i a able to send mail. but problem is that user get n no mails

if i used to take mail.send to out side the function its not work

Rohit Raj
  • 33
  • 2
  • 7
  • You're really being able to send mails? Login into comdummy09@gmail.com and verify that your test emails are in your Sent folder. – Diogo Martins Jan 04 '15 at 02:56

1 Answers1

0

from the docs (http://www.web2py.com/books/default/chapter/29/08/emails-and-sms#Setting-up-email)

Mail returns True if it succeeds in sending the email and False otherwise. A complete argument list for mail.send() is as follows:

So, for a test you should try capturing the response - e.g. change:

mail.send(to=[row.client_owner],

to

mail_response = mail.send(to=[row.client_owner],

and then return locals() instead of "" so you can see in a test page what (if any) failure you are getting in the send. this is just for testing...

A permanent alternate method is shows here: i am trying send email using web2py with gmail and using smtp setting i have attached all code

Community
  • 1
  • 1
tom stratton
  • 668
  • 7
  • 13