通过邮件向特定用户发送多条记录

时间:2015-01-03 06:08:04

标签: python web2py

我需要从数据库向特定用户发送邮件。我有数据表,其中有1000条记录可用。每条记录都有一些电子邮件ID。或者某个id已分配给多个记录。我只需要将记录发送到特定的电子邮件。

我的表单来自于我用来选择公司名称和strt日期。从这些我获取所有值到列表中。现在担心的是如何将记录发送给特定用户

让我的代码示例

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 ''

从这段代码我能够发送邮件。但问题是用户得到没有邮件

如果我曾经把mail.send带到一边,那么这个功能就不起作用了

1 个答案:

答案 0 :(得分:0)

来自文档(http://www.web2py.com/books/default/chapter/29/08/emails-and-sms#Setting-up-email

  

如果成功发送电子邮件,则邮件返回True,然后返回False   除此以外。 mail.send()的完整参数列表如下:

因此,对于测试,您应该尝试捕获响应 - 例如改变:

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

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

然后返回locals()而不是"",这样您就可以在测试页中看到您在发送中遇到的失败(如果有)。这只是为了测试......

此处显示永久替代方法:i am trying send email using web2py with gmail and using smtp setting i have attached all code