当我在那个时候导入数据时,我需要附加pdf报告,该报告将自动发送并声明正文的一部分。如果我发送手动邮件,它会继续带有报告附件。
class import_data(osv.osv):
_name = "import.data"
_columns = {
'csv_file' : fields.binary('CSV FILE', required="1"),
'type':fields.selection([('NSE','NSE'),('BSE','BSE'),('Unlisted','Unlisted')],'Exchange Type',required="1")
}
def send_mail_user(self,cr,uid,ids,data, context=None):
res_user_obj = self.pool.get('res.users')
user = res_user_obj.browse(cr,uid, [uid], context)[0]
mail_mail = self.pool.get('mail.mail')
this_context = context
if data.excess < 0.0:
subject = 'Share Valuation Report for' "%s" % (data.name.name) + ' | Deposite security values'
else:
subject = 'Share Valuation Report for' "%s" % (data.name.name) + ' '
if user.login:
body = '''
Dear Sir/Madam,
<p>Kindly find the share valuation report. The minimum security ratio has been maintained as on <b>''' "%s" % data.write_date +'''</b>.
<p><table>
<tr>
<td>Current Loan :
<b>''' "%s" % data.loan +'''</b>
</td>
<td>Date :
<b>''' "%s" % data.write_date +'''</b>
</td>
</tr>
<tr>
<td>Requisite Security Ratio :
<b>''' "%s" % data.rs_ratio +'''</b>
</td>
<td>Share Type :
<b>''' "%s" % data.type +'''</b>
</td>
</tr>
<tr>
<td>Requisite Security Value :
<b>''' "%s" % data.rs_value +'''</b>
</td>
<td>Excesss/(Shortfall) :
<b>''' "%s" % data.excess +'''</b>
</td>
</tr>
<tr>
<td>Total Share :
<b>''' "%s" % data.total_share +'''</b>
</td>
<td>Number of Share :
<b>''' "%s" % data.no_share +'''</b>
</td>
</tr>
</table></p>
<p>If you have any query/ clairification please contact your Relationship Manager </p>
<p>Regards, </p>
<p>''' "%s" % user.name +''' </p>
<p>''' "%s" % user.email +''' </p>
<p>''' "%s" % user.phone +''' </p>
<p>*** This is an automatically generated email, please do not reply ***</p>
'''
mail_values = {
'email_from': user.email,
'email_to': data.name.email,
'email_cc': user.email,
'subject': subject,
'body_html': body,
'state': 'outgoing',
'type': 'email',
}
mail_id = mail_mail.create(cr, uid, mail_values, context=this_context)
a=mail_mail.send(cr, uid, [mail_id], auto_commit=True, context=this_context)
return True
def import_data(self,cr,uid,ids,context=None):
form = self.read(cr, uid, ids, [])
if not form[0].get('csv_file'):
raise osv.except_osv(_('Warning!'), _('Please Import CSV!'))
fdata = form and base64.decodestring(form[0]['csv_file']) or False
input = cStringIO.StringIO(fdata)
input.seek(0)
try:
data = list(csv.reader(input, quotechar='"' or '"', delimiter=','))
except:
raise osv.except_osv(_('Error!'), _('please imported CSV data file is not properly set for the delimiter !'))
mix_log=""
if data and form[0].get('type') == 'NSE':
self.create_nse(cr,uid,ids,data,context=None)
if data and form[0].get('type') == 'BSE':
self.create_bse(cr,uid,ids,data,context=None)
share_mon_obj= self.pool.get('share.monitors')
his_obj=self.pool.get('share.monitors.history')
share_mon_ids= share_mon_obj.search(cr,uid,[])
vals={}
if share_mon_ids:
for share_id in share_mon_ids:
if share_id:
c_data = share_mon_obj.browse(cr,uid,share_id)
if not c_data.share_line:
raise osv.except_osv(_('Error!'), _('please create the company and share details!'))
for c_line in c_data.share_line:
val={
'company_names' : c_line.names and c_line.names.id or False,
'isin': c_line.isin_no,
'shares' : c_line.share,
'price_quote': c_line.quote_price,
'value_gross': c_line.gross_value,
'ratio_rs': c_line.rs_ratio,
'ratio_as': c_line.as_ratio,
'write_dates' : c_line.write_date,
'monitor_ids': c_line.monitor_id and c_line.monitor_id.id or False,
'share_type':c_line.share_type or False,
}
for csv_line in data:
if form[0].get('type') == 'NSE':
if c_line.isin_no == csv_line[12] and c_line.share_type == 'NSE':
vals={
'quote_price':csv_line[5] or 0.00,
}
his_create_id=his_obj.create(cr,uid,val)
a=self.pool.get('share.monitors.info').write(cr,uid,c_line.id,vals)
if form[0].get('type') == 'BSE':
if c_line.isin_no == csv_line[0] and c_line.share_type == 'BSE':
vals={
'quote_price':csv_line[7] or 0.00,
}
his_create_id=his_obj.create(cr,uid,val)
a=self.pool.get('share.monitors.info').write(cr,uid,c_line.id,vals)
self.send_mail_user(cr, uid,ids,c_data,context=context)
return True
所以你想发送一个带有附件的邮件,所以,首先你想创建一个带有附件报告的模板。
例如:在这里我创建一个模板:
<data noupdate="1">
<!--Email template -->
<record id="email_template_edi_purchase" model="email.template">
<field name="name">RFQ - Send by Email</field>
<field name="email_from">${(object.validator.email or '')|safe}</field>
<field name="subject">${object.company_id.name|safe} Order (Ref ${object.name or 'n/a' })</field>
<field name="partner_to">${object.partner_id.id}</field>
<field name="model_id" ref="purchase.model_purchase_order"/>
<field name="auto_delete" eval="True"/>
<field name="report_template" ref="report_purchase_quotation"/>
<field name="report_name">RFQ_${(object.name or '').replace('/','_')}</field>
<field name="lang">${object.partner_id.lang}</field>
<field name="body_html"><![CDATA[
<div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">
<p>Hello ${object.partner_id.name},</p>
]]></field>
</record>
所以在这个模板中,我在这个模板中附加了我的报告。
现在,您要定义用于发送邮件的函数。下面我定义了一个函数:
def send_purchase_mail(self, cr, uid, ids,active_id, context=None):
mtp =self.pool.get('email.template')
mail_template_id = self.pool.get('ir.model.data').get_object_reference(cr, uid,
'purchase', 'email_template_edi_purchase')
if mail_template_id:
mtp.send_mail(cr, uid, mail_template_id[1], active_id,force_send=True, context=context)
return True
首先在函数中,我得到了我所做的template_id,然后在我用 template_id 和 active_record_id 调用 send_mail 函数之后。