Browse Source

: Dec 04: [FIX] Bug Fixed 'schedule_mail_to_send'

18.0
Risvana Cybro 1 week ago
parent
commit
331362ae7a
  1. 2
      schedule_mail_to_send/__manifest__.py
  2. 5
      schedule_mail_to_send/doc/RELEASE_NOTES.md
  3. 1
      schedule_mail_to_send/models/__init__.py
  4. 59
      schedule_mail_to_send/models/mail_compose_message.py
  5. 30
      schedule_mail_to_send/models/mail_mail.py

2
schedule_mail_to_send/__manifest__.py

@ -21,7 +21,7 @@
############################################################################# #############################################################################
{ {
'name': 'Schedule Mail to Send', 'name': 'Schedule Mail to Send',
'version': '18.0.1.0.0', 'version': '18.0.1.1.0',
'category': 'Discuss', 'category': 'Discuss',
'summary': """Easily schedule your emails for future delivery""", 'summary': """Easily schedule your emails for future delivery""",
'description': """This module provides a function or feature that allows 'description': """This module provides a function or feature that allows

5
schedule_mail_to_send/doc/RELEASE_NOTES.md

@ -4,3 +4,8 @@
#### Version 18.0.1.0.0 #### Version 18.0.1.0.0
##### ADD ##### ADD
- Initial Commit for Schedule Mail to Send - Initial Commit for Schedule Mail to Send
#### 04.12.2025
#### Version 18.0.1.1.0
##### FIX
- Fixed the issue where scheduled emails were sent immediately instead of at the scheduled time.

1
schedule_mail_to_send/models/__init__.py

@ -21,3 +21,4 @@
############################################################################# #############################################################################
from . import mail_compose_message from . import mail_compose_message
from . import mail_activity from . import mail_activity
from . import mail_mail

59
schedule_mail_to_send/models/mail_compose_message.py

@ -57,23 +57,20 @@ class MailComposeMessage(models.TransientModel):
'scheduled_date': self.schedule_time, 'scheduled_date': self.schedule_time,
'recipient_ids': partner_list, 'recipient_ids': partner_list,
'attachment_ids': attachment_list, 'attachment_ids': attachment_list,
'scheduled_user_tz': pytz.timezone(self.env.context.get('tz') or self.env.user.tz),
}) })
# Get the user's timezone (fallback to UTC if not set) # Get the user's timezone (fallback to UTC if not set)
user_tz = self.env.context.get('tz') or self.env.user.tz or 'UTC' user_tz = self.env.context.get('tz') or self.env.user.tz or 'UTC'
# Get current UTC time and convert to user's timezone # Get current UTC time and convert to user's timezone
utc_current_datetime = fields.Datetime.now() # Naive UTC datetime utc_current_datetime = fields.Datetime.now() # Naive UTC datetime
local_current_datetime = fields.Datetime.context_timestamp( local_current_datetime = fields.Datetime.context_timestamp(
self.with_context(tz=user_tz), timestamp=utc_current_datetime self.with_context(tz=user_tz), timestamp=utc_current_datetime
) )
# Handle schedule_time (assumed to be UTC in the database) # Handle schedule_time (assumed to be UTC in the database)
schedule_time = self.schedule_time schedule_time = self.schedule_time
if isinstance(schedule_time, str): if isinstance(schedule_time, str):
# If schedule_time is a string, parse it to datetime (assuming UTC) # If schedule_time is a string, parse it to datetime (assuming UTC)
schedule_time = fields.Datetime.from_string(schedule_time) schedule_time = fields.Datetime.from_string(schedule_time)
# Convert schedule_time to user's timezone # Convert schedule_time to user's timezone
if schedule_time: if schedule_time:
schedule_time = fields.Datetime.context_timestamp( schedule_time = fields.Datetime.context_timestamp(
@ -107,29 +104,31 @@ class MailComposeMessage(models.TransientModel):
return {'type': 'ir.actions.client', 'tag': 'reload'} return {'type': 'ir.actions.client', 'tag': 'reload'}
def action_send_scheduled_mail(self): def action_send_scheduled_mail(self):
"""This function is called by a scheduled action in each minute to """Executed by cron every minute. Sends emails whose scheduled datetime
send the scheduled mails""" has passed according to the timezone of the user who scheduled them.
utc_current_datetime = fields.Datetime.now() """
# Access the time zone Mail = self.env['mail.mail']
user_tz = pytz.timezone(self.env.context.get( # Fetch all mails having schedule date set
'tz') or self.env.user.tz) scheduled_mails = Mail.search([('scheduled_date', '!=', False)])
# Access local time for mail in scheduled_mails:
date_today = pytz.utc.localize(utc_current_datetime).astimezone( # If no timezone stored, fallback to UTC
user_tz) user_tz = mail.scheduled_user_tz or 'UTC'
# Converted to string and removed the utc time difference # Convert current UTC time → user's timezone
user_current_datetime = date_today.strftime( user_local_now = fields.Datetime.context_timestamp(
'%Y-%m-%d %H:%M:%S') mail.with_context(tz=user_tz),
# Again converted to datetime type and replace the seconds with 0 fields.Datetime.now()
user_current_date = datetime.datetime.strptime( ).replace(second=0, microsecond=0)
user_current_datetime, # Convert scheduled UTC → user's timezone
"%Y-%m-%d %H:%M:%S").replace( scheduled_dt = fields.Datetime.context_timestamp(
second=0) mail.with_context(tz=user_tz),
scheduled_mail_rec = self.env['mail.mail'].search( mail.scheduled_date
[('scheduled_date', '<=', user_current_date)]) ).replace(second=0, microsecond=0)
if scheduled_mail_rec: # If scheduled time has passed, send mail
for record in scheduled_mail_rec: if scheduled_dt <= user_local_now:
record.send() mail.send()
planned_activity = self.env['mail.activity'].search( # Remove any linked scheduled activity
[('schedule_mail_id', '=', record.id)]) activity = self.env['mail.activity'].search(
# unlink the planned activity [('schedule_mail_id', '=', mail.id)]
planned_activity.sudo().action_feedback(self) )
if activity:
activity.sudo().action_feedback()

30
schedule_mail_to_send/models/mail_mail.py

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2025-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
# Author: Cybrosys Techno Solutions(<https://www.cybrosys.com>)
#
# You can modify it under the terms of the GNU LESSER
# GENERAL PUBLIC LICENSE (LGPL v3), Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# (LGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
from odoo import fields, models
class MailMail(models.Model):
"""This class is used to add fields to mail.mail model to"""
_inherit = 'mail.mail'
scheduled_user_tz = fields.Char(string="Scheduled User Timezone",
help="Shows the timezone of the user who scheduled the mail")
Loading…
Cancel
Save