2

i know we can create automatic action using cron in odoo but I want something a different in the mass mailing of odoo i want to add a repetion option of mail mass mailings Example in the Form view_mail_mass_mailing_form > Options page
I added a repetition selection field, I added this because I want each mass mail alone

class MailMassMailing(models.Model):
_inherit = 'mail.mass_mailing' 

recurrence_mail = fields.Selection([
    ('daily', 'Day'),
    ('weekly', 'Weeks'),
    ('monthly', 'Months'),
], string='Recurring')

I want this mass mailng to send each (days or weeks or months) how to call a function with interval date, how to call a function every (days or weeks or months)

The sending of this mass mailing is revived from the date of creation

Boubaker
  • 427
  • 1
  • 7
  • 24
  • Where is the problem to write a method used for a cron job in Odoo which searches for mass mailings with such an option? You should add a sent_last date field for a cron solution, to find out which mass mailing has to be sent again. Don't reinvent the wheel and try to use existing functionality in Odoo. – CZoellner Feb 07 '19 at 12:55
  • yes I like working with existing functionality in Odoo but the cron job does not answer my needs because I want to put a different value (day, week, month) for each mass mailing – Boubaker Feb 07 '19 at 13:00
  • But you can do that. Just extend the mass mailing model with your interval and a date field to indicate when it was last sent. Then create a cron job (ir.cron) calling a method (written by you as model method on mass mailing) which should just iterate through every mass mailing and send every one again if interval and last sent date "matches". It's not a fancy solution, but that should work. – CZoellner Feb 07 '19 at 13:03
  • Your idea can work But my problem is how to create these methode with interval date – Boubaker Feb 07 '19 at 13:29
  • I will write an answer, but please don't wait for it. Sometimes i just forget it :( – CZoellner Feb 07 '19 at 14:00

2 Answers2

2

Just extend Mass Mailing model with a new date field and implement a model method to use for a daily running ir.cron.

from odoo import api, fields, models

class MailMassMailing(models.Model):
    _inherit = 'mail.mass_mailing' 

    recurrence_mail = fields.Selection([
        ('daily', 'Day'),
        ('weekly', 'Weeks'),
        ('monthly', 'Months'),
    ], string='Recurring')
    last_sent_on = fields.Date()

    @api.model
    def run_send_recurring(self):
        """ Resend mass mailing with recurring interval"""
        domain = [('recurrence_mail', '!=', False)]
        # TODO monthly should be solved in another way, but that
        # is not needed for this example
        deltas = {'daily': 1, 'weekly': 7, 'monthly': 30}
        today = fields.Date.today()
        for mass_mail in self.search(domain):
            # never sent? go send it
            if not mass_mail.last_sent_on:
                # send the way you want
            # or get delta between today and last_sent_on
            last_dt = fields.Date.from_string(mass_mail.last_sent_on)
            if (today - last_dt).days >= deltas[mass_mail.recurrence_mail]:
                # send the way you want
CZoellner
  • 13,553
  • 3
  • 25
  • 38
0

Thank you @CZoellner for your help I found the solution with your idea

# Solution ############### .py
@api.model
def run_send_recurring(self):
    """ Resend mass mailing with recurring interval"""
    date_format = '%Y-%m-%d'
    domain = [('recurrence_mail', '!=', False),('state','=','done')]
    deltas = {'daily': 1, 'weekly': 7, 'monthly': 30}
    logger.info("______deltas________: %s ",deltas)
    today = fields.Date.today()
    logger.info("______today________: %s ",today) 
    for mass_mail in self.search(domain):
        logger.info("______mass_mail________: %s ",mass_mail)
        # never sent? go send it
        if not mass_mail.last_sent_on:
            self.put_in_queue()  

        joining_date = mass_mail.last_sent_on
        current_date = (datetime.today()).strftime(date_format)
        print('joining_date',joining_date)
        d1 = datetime.strptime(joining_date, date_format).date()
        logger.info("______1 day________: %s ",d1)
        d2 = datetime.strptime(current_date, date_format).date()
        logger.info("______2 day________: %s ",d2)
        logger.info("______deltas[mass_mail.recurrence_mail]________: %s ",deltas[mass_mail.recurrence_mail])
        r = relativedelta(d1,d2)    
        logger.info("______r day________: %s ",r.days)  
        if (r ,'>=' ,  deltas[mass_mail.recurrence_mail]):
            mass_mail.put_in_queue()
Boubaker
  • 427
  • 1
  • 7
  • 24