Templated mail sending method and device, electronic equipment and program product
By using a templated email sending method and leveraging a distributed caching cluster and resource locking mechanism, the problem of independence in multi-module email sending is solved, enabling unified scheduling and management of email sending and improving the system's flexibility and reliability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-10
- Publication Date
- 2026-04-07
AI Technical Summary
In existing technologies, the email sending functions of each module in the fare publication business are independent and redundant, resulting in high maintenance costs, waste of resources, and problems such as duplicate or failed emails.
A templated email sending method is adopted. Multiple service nodes are started within the service cycle, template information is loaded from the distributed cache cluster, the target service node is determined based on resource locks, the set of email tasks is obtained, and emails are generated and sent on the target service node. The distributed lock mechanism is used to solve race conditions and realize the unified scheduling and management of email tasks.
It achieves standardization, flexibility, and reliability in email sending, reduces maintenance costs, avoids duplicate emails and failures, and improves the accuracy and efficiency of email sending.
Smart Images

Figure CN121814726A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of electronic information technology, and more specifically, to a method and apparatus for sending templated emails, an electronic device, and a program product. Background Technology
[0002] Currently, each module of the fare publication service has its own independent email sending function. Each module's email function differs in implementation, configuration management, and email content. Maintenance personnel need to maintain multiple sets of email sending logic separately, and any modification to common configurations must be performed on each module individually, which is prone to omissions and errors, increasing system maintenance costs and risks. Furthermore, each module requires separate server resources and network bandwidth to handle email sending tasks, making resource reuse and unified scheduling impossible. Additionally, independent email sending functions may result in end users receiving emails from different system modules with duplicate or similar content, causing information redundancy, or email sending failures due to operational conflicts.
[0003] There is currently no effective solution to the above problems. Summary of the Invention
[0004] This invention provides a method, apparatus, electronic device, and program product for sending templated emails, to at least solve the technical problem in related technologies where the lack of a service scheduling and coordination mechanism leads to emails failing to be sent correctly.
[0005] According to one aspect of the present invention, a method for sending templated emails is provided, applied to an email service system, comprising: starting multiple service nodes within a current service cycle; for each service node, loading all template information from a distributed cache cluster and storing all template information in the cache of the service node, wherein the distributed cache cluster stores template information for multiple email templates; controlling all service nodes to request resource locks from the distributed cache cluster, and determining a target service node based on the resource locks; controlling the target service node to query a preset database to obtain a set of email tasks to be processed, wherein the set of email tasks to be processed includes at least: a first type of email task and a second type of email task, the first type of email task being email tasks added within the current service cycle, and the second type of email task being email tasks that failed to be processed in the previous service cycle; for each email task to be processed in the set of email tasks to be processed, searching for template information associated with the email task to be processed from the cache; if template information associated with the email task to be processed is found, generating an email based on the email task to be processed and the template information, and sending the email to the recipient's mailbox.
[0006] Furthermore, the step of determining the target service node based on the resource lock includes: if any service node successfully acquires the resource lock, writing a preset field to the distributed cache cluster, wherein the preset field is used to characterize the owner of the resource lock and the resource lock has a corresponding lifespan; and determining the service node as the target service node.
[0007] Furthermore, after determining the target service node based on the resource lock, the method also includes: constructing a monitoring mechanism, wherein the monitoring mechanism monitors at a preset frequency whether the target service node has finished processing the set of pending email tasks; if the target service node has not finished processing the set of pending email tasks, the lifespan of the resource lock is increased.
[0008] Furthermore, before retrieving the template information associated with the pending email task from the cache, the process includes: constructing multiple email templates based on template type and email information, wherein each email template includes at least a first preset template and a second preset template. The first preset template includes at least sender information and network information, and the second preset template is an email framework including recipient information and the content received by the recipient. The first preset template is associated with multiple second preset templates, and the email templates are associated with the pending email task through template type; all email templates are stored in a preset database, and all email templates in the preset database are loaded into the distributed cache cluster.
[0009] Furthermore, the steps of constructing multiple email templates based on template type and email information include: constructing a first preset template based on template type, sender information, host address and proxy server information; and constructing a second preset template based on template type, recipient information, template content and template title.
[0010] Furthermore, the email task to be processed includes at least: task information. The steps for generating an email based on the email task to be processed and the template information include: replacing multiple preset placeholders in the second preset template based on the email content and attachment content in the task information, wherein the email content includes at least: a task identifier, title content, and body content, and the preset placeholders include at least: a preset placeholder for the template title and a preset placeholder for the template content; replacing the preset placeholder for the template title based on the title content, and replacing the preset placeholder for the template content based on the body content, to obtain a replaced second preset template; determining the first preset template corresponding to the replaced second preset template, and generating an email based on the first preset template and the replaced second preset template.
[0011] Furthermore, after sending the email to the recipient's mailbox, the process also includes: in the event of email sending failure, classifying the pending email task corresponding to the email as a second type of email task and adding the second type of email task back to the preset database; releasing the resource lock, and after releasing the resource lock, controlling all service nodes to re-request the resource lock from the distributed cache cluster in order to process the email task.
[0012] According to another aspect of the present invention, a templated email sending apparatus is also provided, applied to an email service system, comprising: a loading unit, configured to start multiple service nodes within the current service cycle, load all template information from a distributed cache cluster for each service node, and store all template information in the cache of the service node, wherein the distributed cache cluster stores template information of multiple email templates; an acquisition unit, configured to control all service nodes to request resource locks from the distributed cache cluster, and determine a target service node based on the resource locks, and control the target service node to query a preset database to acquire a set of email tasks to be processed, wherein the set of email tasks to be processed includes at least: a first type of email task and a second type of email task, the first type of email task being email tasks added within the current service cycle, and the second type of email task being email tasks that failed to be processed in the previous service cycle; a search unit, configured to search for template information associated with the email task to be processed from the cache for each email task to be processed in the set of email tasks to be processed; and a generation unit, configured to generate an email based on the email task to be processed and the template information when template information associated with the email task to be processed is found, and send the email to the recipient's mailbox.
[0013] Furthermore, the acquisition unit includes: a first writing module, used to write a preset field to the distributed cache cluster when any service node successfully acquires the resource lock, wherein the preset field is used to characterize the owner of the resource lock and the resource lock has a corresponding lifespan; and a first determining module, used to determine the service node as the target service node.
[0014] Furthermore, the templated email sending device also includes: a first construction module, used to construct a monitoring mechanism after determining the target service node based on the resource lock, wherein the monitoring mechanism monitors at a preset frequency whether the target service node has finished processing the set of pending email tasks; and a first addition module, used to increase the lifespan of the resource lock if the target service node has not finished processing the set of pending email tasks.
[0015] Furthermore, the templated email sending device also includes: a second construction module, used to construct multiple email templates based on template type and email information before searching for template information associated with the email task to be processed from the cache, wherein the email template includes at least: a first preset template and a second preset template, the first preset template including at least: sender information and network information, the second preset template being an email framework including recipient information and recipient received content, the first preset template being associated with multiple second preset templates, and the email templates being associated with the email task to be processed through template type; and a first loading module, used to store all email templates in a preset database and load all email templates in the preset database into a distributed cache cluster.
[0016] Furthermore, the second construction module includes: a first construction submodule, used to construct a first preset template based on template type, sender information, host address and proxy server information; and a second construction submodule, used to construct a second preset template based on template type, recipient information, template content and template title.
[0017] Furthermore, the email task to be processed includes at least: task information, and the generation unit includes: a first replacement module, used to replace multiple preset placeholders in the second preset template based on the email content and attachment content in the task information, wherein the email content includes at least: task identifier, title content, and body content, and the preset placeholders include at least: preset placeholders for the template title and preset placeholders for the template content; a second replacement module, used to replace the preset placeholders for the template title based on the title content, and to replace the preset placeholders for the template content based on the body content, to obtain the replaced second preset template; and a first generation module, used to determine the first preset template corresponding to the replaced second preset template, and to generate an email based on the first preset template and the replaced second preset template.
[0018] Furthermore, the templated email sending device also includes: a second determining module, used to determine the pending email task corresponding to the email as a second type of email task in the event of email sending failure after the email is sent to the recipient's mailbox, and to re-add the second type of email task to the preset database; and a first releasing module, used to release the resource lock, and after releasing the resource lock, to control all service nodes to re-request the resource lock from the distributed cache cluster in order to process the email task.
[0019] According to another aspect of the present invention, a computer program product is also provided, including a non-volatile computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the method for sending templated emails as described above.
[0020] According to another aspect of the present invention, an electronic device is also provided, including one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement any of the above-described methods for sending templated emails.
[0021] In this invention, multiple service nodes are started within the current service cycle. For each service node, all template information is loaded from the distributed cache cluster and stored in the service node's cache. All service nodes are controlled to request resource locks from the distributed cache cluster. Based on the resource locks, a target service node is determined, and the target service node is controlled to query a preset database to obtain a set of email tasks to be processed. For each email task to be processed in the set, the template information associated with the email task to be processed is searched from the cache. If the template information associated with the email task to be processed is found, an email is generated based on the email task to be processed and the template information, and the email is sent to the recipient's mailbox. This solves the technical problem in related technologies where the lack of a service scheduling and coordination mechanism leads to emails not being sent correctly.
[0022] In this invention, at the start of each service cycle, the email service system can launch multiple service nodes. These nodes then load all template information from the distributed cache cluster and store it in their local cache. The distributed cache cluster pre-stores metadata for various email templates. Then, these nodes can request resource locks from the distributed cache cluster. Resource lock acquisition is based on a first-come, first-served principle; the service node that successfully acquires the lock is designated as the target service node. By using resource locks, race conditions (i.e., in a distributed system, when multiple processes, threads, or service instances simultaneously access the same resource or data, leading to data inconsistency, duplicate operations, or erroneous results without synchronization or locking mechanisms) are avoided. This ensures that email tasks can only be processed by the target service node. The target service node can then access a pre-defined database to perform a task query to obtain a set of email tasks to be processed in the current period. This set of tasks includes not only the first type of email tasks newly added in the current period, but also the second type of email tasks that failed to be processed in the previous service period for various reasons. For each email task to be processed in the set, the target service node can search for matching template information in its cache. If a template information associated with the email task to be processed is found, a personalized email can be dynamically generated using the template information and the detailed content of the email task, and then sent to the recipient's mailbox, improving the accuracy and reliability of email delivery. Attached Figure Description
[0023] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this invention, illustrate exemplary embodiments of the invention and are used to explain the invention, but do not constitute an undue limitation of the invention. In the drawings:
[0024] Figure 1 This is a flowchart of an optional method for sending templated emails according to an embodiment of the present invention;
[0025] Figure 2 This is a schematic diagram of an optional email sending function module before business decoupling according to an embodiment of the present invention;
[0026] Figure 3 This is a schematic diagram of an optional email sending function after business decoupling according to an embodiment of the present invention;
[0027] Figure 4 This is a flowchart of an optional email service according to an embodiment of the present invention;
[0028] Figure 5 This is a flowchart of an optional service scheduling and coordination method according to an embodiment of the present invention;
[0029] Figure 6 This is a schematic diagram of an optional email template according to an embodiment of the present invention;
[0030] Figure 7 This is an overall architecture diagram of an optional email sending method according to an embodiment of the present invention;
[0031] Figure 8 This is a schematic diagram of an optional templated email sending device according to an embodiment of the present invention;
[0032] Figure 9 This is a hardware structure block diagram of an electronic device (or mobile device) for sending templated emails according to an embodiment of the present invention. Detailed Implementation
[0033] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0034] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0035] It should be noted that all related information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, and displayed data) collected and involved in this invention are information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of this data comply with the relevant laws, regulations, and standards of the relevant regions, necessary confidentiality measures have been taken, and it does not violate public order and good morals. Corresponding operation entry points are provided for users to choose to authorize or refuse. For example, this system has an interface with relevant users or organizations. Before obtaining relevant information, a request to obtain the information needs to be sent to the aforementioned user or organization through the interface, and the relevant information is obtained only after receiving consent from the aforementioned user or organization.
[0036] In this invention, by separating the recipient template, sender template, and email task, the email content, sending configuration, and sending task are decoupled. A single sender template can be used for multiple different recipient templates using the same template type. This decoupling enhances the system's flexibility and maintainability, enabling centralized management and efficient reuse of sending resource configurations. Furthermore, in a microservice multi-node deployment environment, a distributed lock mechanism resolves the race condition problem of task duplication, ensuring that the same email task is not executed repeatedly, thus ensuring business accuracy and reliability and preventing duplicate emails from inconveniencing users. In addition, all configurations are centralized in the database management interface, allowing operations personnel to intuitively browse, modify, and manage configurations without directly editing complex code or configuration files. A unified email standard reduces operational burden, and sensitive information can be encrypted and stored, improving data security.
[0037] The present invention will now be described in detail with reference to various embodiments.
[0038] Example 1
[0039] According to an embodiment of the present invention, an embodiment of a method for sending templated emails is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0040] Figure 1 This is a flowchart of an optional method for sending templated emails according to an embodiment of the present invention, such as... Figure 1 As shown, the method includes the following steps:
[0041] Step S101: Start multiple service nodes within the current service cycle. For each service node, load all template information from the distributed cache cluster and store all template information in the service node's cache. The distributed cache cluster stores template information for multiple email templates.
[0042] Optionally, the email service system is a scheduled task-based service system that can query the database at set intervals to retrieve email tasks that need to be processed, assemble the emails, and send them to the email server to achieve email sending.
[0043] In this embodiment, multiple service nodes can be started within the current service cycle. Each service node can load all template information from a distributed cache cluster (such as a Redis cluster) and store all template information in the service node's cache. The template information is predefined and stored in the distributed cache cluster.
[0044] Figure 2 This is a schematic diagram of an optional email sending function module before business decoupling according to an embodiment of the present invention, as shown below. Figure 2 As shown, before the application of the email service system, the email function of the fare publication business had a tightly coupled architecture. Each business module (i.e., module 1, module 2, ..., module N) had its own independent email sending function (i.e., email function 1, email function 2, ..., email function n). Each email sending function handled the corresponding business (i.e., business 1, business 2, ..., business n). When it was necessary to update the email sending configuration, it had to be done separately in each business module, and then the email sending business was implemented through the SMTP server (SimpleMail Transfer Protocol Server).
[0045] Figure 3 This is a schematic diagram of an optional email sending function after business decoupling according to an embodiment of the present invention, such as... Figure 3As shown, each business module (i.e., module 1, module 2, ..., module N) has a corresponding business (i.e., business 1, business 2, ..., business n). The business module does not need to consider how the email is delivered, but only needs to record an email task in the database. The functions of the email service include loading data (i.e. loading template information in the distributed cache cluster), obtaining email tasks, sending emails and recording emails, and then implementing the email sending business through the SMTP server.
[0046] Step S102: Control all service nodes to request resource locks from the distributed cache cluster, and based on the resource locks, determine the target service node, control the target service node to query the preset database, and obtain the set of email tasks to be processed. The set of email tasks to be processed includes at least: a first type of email task and a second type of email task. The first type of email task is the email task added in the current service cycle, and the second type of email task is the email task that failed to be processed in the previous service cycle.
[0047] In this embodiment, all service nodes can apply for resource locks (i.e., global locks) from the distributed cache cluster. Only the service (i.e. the target service node) that successfully acquires the resource lock can continue to execute subsequent task fetching. Thus, in a distributed multi-instance deployment environment, only one service instance can process tasks at any given time, avoiding the problems of repeated task fetching and race conditions in task execution.
[0048] For example, an email service system has three email service nodes, A, B, and C, which start at 1:00 AM daily (the beginning of the current service cycle). All nodes attempt to acquire a resource lock named "mailServiceLock" from the Redis cluster. Node A acquires the lock first, and can then be designated as the target service node, responsible for executing email task fetching and processing within this cycle. Nodes B and C suspend email task processing until they attempt to acquire the resource lock again in the next cycle.
[0049] The target service node can query the preset database (i.e., the database used to store email tasks) to retrieve the set of email tasks to be processed. The set of email tasks to be processed includes at least the email tasks newly generated in the current service cycle (first type of email tasks) and the email tasks that failed to be sent in the previous service cycle (second type of email tasks). This achieves the integrity and continuity of email task processing, enabling the processing of new tasks while making up for possible omissions or failures in the previous cycle, thus enhancing the reliability of the system.
[0050] Step S103: For each pending email task in the set of pending email tasks, retrieve the template information associated with the pending email task from the cache.
[0051] In this embodiment, for each captured email task to be processed, the template information associated with the task can be found in the cache of the target service node. The template information may include the email style template, the title template, and placeholders for receiving dynamic content.
[0052] For example, email task a corresponds to the "password reset" scenario. Node A can search for relevant template information of the "password reset" template type in the cache, including predefined email title and body format, as well as placeholders such as {userName}, {resetLink}, etc.
[0053] Step S104: If template information associated with the email task to be processed is found, an email is generated based on the email task to be processed and the template information, and the email is sent to the recipient's email address.
[0054] In this embodiment, when template information associated with the email task to be processed is found, the template information and dynamic data in the email task to be processed (such as the actual username, reset link, etc.) can be used to generate the content of the specific email. Based on the email data in the email task, the email is sent to the designated SMTP server. The SMTP server forwards the email to the server where the recipient's mailbox is located according to the SMTP protocol, and finally delivers the email to the recipient's mailbox.
[0055] Figure 4 This is a flowchart of an optional email service according to an embodiment of the present invention, such as... Figure 4 As shown, firstly, the operations and maintenance personnel pre-store email template information and email tasks (i.e., email tasks to be processed, which can be submitted by multiple related business modules) in the database. The email service starts on a regular schedule and can periodically update the template information to the distributed cache to facilitate subsequent data retrieval from the distributed cache. Then, the email service can query the database on a regular schedule according to the settings to retrieve email tasks. There are two types of retrieval: one is newly stored email tasks to be processed by business modules, and the other is email tasks that timed out or failed to be sent during the last sending. After that, according to the template type of the email task to be processed, the corresponding template information is matched, assembled, and emails are generated. It can also connect to the corresponding SMTP server and proxy server to send emails based on the email data in the email task to be processed, and record the email information (i.e., record the processing status of the email in the database; if there is data that failed to be sent, it will be resent when the task is started next time), and then the service ends.
[0056] In summary, by starting multiple service nodes within the current service cycle and loading template information from the distributed caching cluster into the local cache, and then requesting resource locks from the distributed caching cluster to determine the target service node, the target service node can obtain a set of pending email tasks from a preset database, including newly generated email tasks and email tasks that failed to be processed in the previous cycle, ensuring the integrity and continuity of email sending. Then, based on the template type associated with the task, template information is retrieved from the target service node's cache. Combined with dynamic data from the email tasks, personalized email content is automatically generated, ultimately sending emails to recipients' mailboxes efficiently and accurately. This achieves standardization and template-based email sending, improves the flexibility and reliability of the email service, resolves race conditions in multi-node deployments, and enhances the accuracy and efficiency of the system's email sending.
[0057] To ensure the uniqueness of the target service node, in the templated email sending method provided in Embodiment 1 of this application, when any service node successfully acquires a resource lock, a preset field is written to the distributed cache cluster. The preset field is used to characterize the owner of the resource lock, and the resource lock has a corresponding lifespan. The service node is then identified as the target service node.
[0058] In this embodiment, when any service node successfully acquires a global resource lock from the distributed cache cluster, it immediately writes a preset field to the cache cluster, which can then be used to identify the service node as the target service node. The preset field includes the lock owner's identification information (i.e., the ID or name of the current service node), indicating which service node currently owns and is using the resource lock. The resource lock itself is configured with a lifetime, such as 30 seconds, indicating that the lock will not be held indefinitely to prevent node failure from causing the lock to fail to be released normally, thus achieving high availability of the system.
[0059] To improve the stability and accuracy of email processing, a monitoring mechanism is constructed in the templated email sending method provided in Embodiment 1 of this application. The monitoring mechanism monitors at a preset frequency whether the target service node has finished processing the set of pending email tasks. If the target service node has not finished processing the set of pending email tasks, the lifespan of the resource lock is increased.
[0060] In this embodiment, a monitoring mechanism (such as a watchdog mechanism) can be built in the email service system. This mechanism checks periodically at a preset frequency (e.g., every 10 seconds) whether the target service node has completed the processing of the set of email tasks to be processed in the current service cycle. If the target service node has not completed the processing of the set of email tasks to be processed, the lifespan of the resource lock is increased, such as by 10 seconds, so that the target service node has enough time to complete its tasks without interrupting the processing due to the early expiration of the lock. By introducing a monitoring mechanism and conditionally extending the lifespan of the resource lock, the flexibility of email processing is enhanced.
[0061] Figure 5 This is a flowchart of an optional service scheduling and coordination method according to an embodiment of the present invention, such as... Figure 5 As shown, firstly, all service nodes (node 1, node 2, ..., node n) start simultaneously according to the settings and request resource locks from the distributed cache cluster (i.e., acquire locks). The distributed cache cluster consists of multiple distributed cache cluster nodes used to store data. Service nodes that successfully acquire the lock (i.e., successfully acquire locks) execute tasks (i.e., pending email tasks). After the task is completed, the lock is deleted. At the same time, a watchdog mechanism can be used to monitor whether the task holds the lock. If the service node has not finished processing the pending email tasks, the lock's lifespan is extended. Service nodes that fail to acquire the lock do not perform business processing and wait for the next round of acquisition.
[0062] To achieve templated configuration and storage of email functionality, in the templated email sending method provided in Embodiment 1 of this application, multiple email templates are constructed based on template type and email information. Each email template includes at least a first preset template and a second preset template. The first preset template includes at least sender information and network information. The second preset template is an email framework that includes recipient information and the content received by the recipient. The first preset template is associated with multiple second preset templates. The email templates are associated with the email tasks to be processed through template type. All email templates are stored in a preset database, and all email templates in the preset database are loaded into a distributed cache cluster.
[0063] In this embodiment, based on different email usage scenarios and needs, multiple email templates can be created based on template types (e.g., customer service template types). The email templates include at least two types: one is the sender template (i.e., the first preset template), which contains sender information (e.g., sender's email address, name) and network information (e.g., SMTP server address, port, authentication protocol, etc.); the other is the recipient template (i.e., the second preset template), which contains recipient information (recipient's email address, salutation, etc.) and an email frame containing the content received by the recipient. The frame reserves dynamic content placeholders, such as {username}, {password}, etc.
[0064] The first preset template can be associated with multiple second preset templates. For example, if the template type is a customer service template, its sending template can be associated with multiple receiving templates (such as password reset templates, welcome email templates, etc.). Email templates can be associated with email tasks to be processed through template types. By separating sending templates and receiving templates and establishing associations based on template types, the system can flexibly select appropriate email templates according to different scenarios. At the same time, it realizes the unified standard and personalized content of email sending. All email templates can be stored in a preset database, and all email templates in the preset database can be loaded into a distributed cache cluster so that the target service nodes can access and use email template information faster, reduce database query pressure, and improve response speed.
[0065] In order to accurately construct email templates, in the templated email sending method provided in Embodiment 1 of this application, a first preset template is constructed based on the template type, sender information, host address and proxy server information; and a second preset template is constructed based on the template type, recipient information, template content and template title.
[0066] Optionally, an SMTP server can transmit emails from the sender to the recipient's mailbox server. It is the basic protocol for Internet email transmission. During the connection to the SMTP server, when the network where the email sending service is located has limited access to the SMTP server, a proxy server can be used to bypass the network restrictions, or to perform additional processing or verification on the data before sending the email.
[0067] In this embodiment, the first preset template is used to define the identity and network configuration of the sending email. It can be constructed based on template type (a key identifier distinguishing different email categories, e.g., for customer service emails, the template type could be CustomerService), sender information (e.g., sender's email address, sender's name, etc.), the host address and port number of the SMTP server, security protocols (e.g., TLS (Transport Layer Security), SSL (Secure Sockets Layer), etc.), and proxy server information. The second preset template is a personalized email framework based on the email usage scenario and recipient group. It can be constructed based on template type, recipient information (e.g., recipient's email address, recipient's name, etc.), template content, and template title. By decomposing the sending elements of the email and constructing them separately into the first and second preset templates, the network parameters and email content for sending emails can be managed independently, simplifying template maintenance and improving the flexibility and efficiency of email sending.
[0068] The email task to be processed includes at least: task information. In order to accurately generate emails, in the templated email sending method provided in Embodiment 1 of this application, multiple preset placeholders in the second preset template are replaced based on the email content and attachment content in the task information. The email content includes at least: task identifier, title content, and body content. The preset placeholders include at least: preset placeholders for the template title and preset placeholders for the template content. The preset placeholders for the template title are replaced according to the title content, and the preset placeholders for the template content are replaced according to the body content to obtain the replaced second preset template. The first preset template corresponding to the replaced second preset template is determined, and the email is generated based on the first preset template and the replaced second preset template.
[0069] Figure 6 This is a schematic diagram of an optional email template according to an embodiment of the present invention, such as... Figure 6 As shown, the system includes recipient templates, sender templates, email tasks, attachment content, and email content. Recipient templates are customizable email frameworks based on email usage scenarios and recipient groups, including: template type, recipients, subject templates, and style templates. Sender templates define the sender's identity and network configuration, including: template type, host address, sender email address, security protocol, and proxy settings. Email content stores the actual content after replacing template placeholders; its data table fields include at least: task information (e.g., task ID), subject content, and body content. Attachment content stores attachment information; its data table fields include at least: task information and attachment information (e.g., attachment name, attachment storage information). Email content and attachment content are associated with email tasks via task IDs, while recipient and sender templates are associated with email tasks via template types. Email tasks record a specific sending request and include at least: task information, template type, task status, creation time, and planned sending time.
[0070] In this embodiment, when the target service node retrieves the pending email tasks from the database, it can first parse the task information. The task information can include email content and attachment content. The data table fields of the email content can include associated task identifiers, title content, body content, etc., and can be associated with the pending email tasks through the task identifier. The data table fields of the attachment content can include associated task identifiers, attachment names, attachment storage information, etc., and can be associated with the pending email tasks through the task identifier. Based on the email content and attachments in the task information, multiple preset placeholders in the second preset template (such as preset placeholders for the template title and template content) can be replaced. Based on the title content, the preset placeholders for the template title can be replaced, and based on the body content, the preset placeholders for the template content can be replaced, resulting in a replaced second preset template. Based on the template type, the corresponding first preset template can be determined. Based on the sender information and network information of the first preset template, as well as the replaced second preset template, an email (including email headers (sender, recipient, CC, BCC, subject, etc.), email body, and attachments) can be generated. This realizes intelligent association and dynamic content filling between email tasks and email templates, improving the automation and accuracy of email sending.
[0071] Figure 7 This is an optional overall architecture diagram for email sending according to an embodiment of the present invention, such as... Figure 7 As shown, it includes a service scheduling layer, an email construction layer, and a business processing layer. First, the service scheduling layer can schedule multiple service nodes (e.g., service node 1, service node 2, service node 3, ..., service node N) through a scheduler to determine the target service node. The email construction layer can construct the receiving template, sending template, email task, email content, and email attachments, and persistently store their data in the database. The target service node can process the email tasks stored in the database and generate emails. After that, the business processing layer sends emails by connecting to the server and records the email information in the database. If there is any data that fails to be sent, it can be resent in the next attempt.
[0072] To improve the success rate of email delivery, in the templated email delivery method provided in Embodiment 1 of this application, in the event of email delivery failure, the pending email task corresponding to the email is identified as a second type of email task, and the second type of email task is re-added to the preset database; the resource lock is released, and after the resource lock is released, all service nodes are controlled to re-request the resource lock from the distributed cache cluster in order to process the email task.
[0073] In this embodiment, when the email service system encounters a sending failure during the email sending process, such as an SMTP server returning an error code, a network connection timeout, or the email being rejected, the system will identify the failed email. For the identified failed email, the system can mark its corresponding pending email task as a second-type email task, indicating that the email task needs to be reprocessed later. Then, this second-type email task can be added back to the preset database so that it can be sent again in the next round of task fetching. By adding the failed email task back to the preset database and processing it again in the next cycle, the email sending success rate and system stability are improved. At the same time, after processing all tasks in the pending email task set, the resource lock is released. After the resource lock is released, all service nodes can re-request the resource lock from the distributed cache cluster to process the email tasks in the next cycle, realizing timely task processing and efficient utilization of system resources.
[0074] In this embodiment of the invention, by constructing a decoupled microservice architecture and implementing a distributed lock mechanism, the standardization, automation, and efficient management of email functionality are achieved. Specifically, through preset sender and receiver templates, a high degree of customization and unified standardization of email content and sending configuration is achieved, realizing the flexibility of email services. Resource locks in a distributed cache cluster resolve race conditions in multi-node deployments. Furthermore, through monitoring mechanisms and dynamic adjustment of resource lock lifetimes, the continuity of task processing and the flexibility of the system are enhanced. Moreover, the marking and storage of task status when email sending fails ensures the reliability of email delivery, avoids data loss, and improves the accuracy of email sending.
[0075] The following is a detailed description with reference to another embodiment.
[0076] Example 2
[0077] The templated email sending device provided in this embodiment includes multiple implementation units, each of which corresponds to a specific implementation step in Embodiment 1 above.
[0078] Figure 8 This is a schematic diagram of an optional templated email sending device according to an embodiment of the present invention, such as... Figure 8 As shown, the templated email sending device may include: a loading unit 80, an acquisition unit 81, a search unit 82, and a generation unit 83.
[0079] The loading unit 80 is used to start multiple service nodes within the current service cycle. For each service node, it loads all template information from the distributed cache cluster and stores all template information in the cache of the service node. The distributed cache cluster stores template information for multiple email templates.
[0080] The acquisition unit 81 is used to control all service nodes to apply for resource locks from the distributed cache cluster, and based on the resource locks, determine the target service node, control the target service node to query the preset database, and obtain the set of email tasks to be processed. The set of email tasks to be processed includes at least: a first type of email task and a second type of email task. The first type of email task is the email task added in the current service cycle, and the second type of email task is the email task that failed to be processed in the previous service cycle.
[0081] The lookup unit 82 is used to look up template information associated with each pending email task in the set of pending email tasks from the cache.
[0082] The generation unit 83 is used to generate an email based on the email task to be processed and the template information when the template information associated with the email task to be processed is found, and to send the email to the recipient's mailbox.
[0083] The aforementioned templated email sending device can start multiple service nodes within the current service cycle through the loading unit 80. For each service node, it loads all template information from the distributed cache cluster and stores all template information in the service node's cache. The acquisition unit 81 can control all service nodes to request resource locks from the distributed cache cluster and, based on the resource locks, determine the target service node and control the target service node to query the preset database to obtain the set of email tasks to be processed. The search unit 82 can search for the template information associated with each email task in the set of email tasks to be processed from the cache. The generation unit 83 can generate an email based on the email task to be processed and the template information when the template information associated with the email task to be processed is found, and send the email to the recipient's mailbox.
[0084] Optionally, the acquisition unit 81 includes: a first writing module, used to write a preset field to the distributed cache cluster when any service node successfully acquires the resource lock, wherein the preset field is used to characterize the owner of the resource lock and the resource lock has a corresponding lifespan; and a first determining module, used to determine the service node as the target service node.
[0085] Optionally, the templated email sending device further includes: a first construction module, used to construct a monitoring mechanism after determining the target service node based on the resource lock, wherein the monitoring mechanism monitors at a preset frequency whether the target service node has finished processing the set of pending email tasks; and a first addition module, used to increase the lifespan of the resource lock if the target service node has not finished processing the set of pending email tasks.
[0086] Optionally, the templated email sending device further includes: a second building module, used to build multiple email templates based on template type and email information before searching for template information associated with the email task to be processed from the cache, wherein the email template includes at least: a first preset template and a second preset template, the first preset template includes at least: sender information and network information, the second preset template is an email framework including recipient information and recipient received content, the first preset template is associated with multiple second preset templates, and the email template is associated with the email task to be processed through template type; and a first loading module, used to store all email templates in a preset database and load all email templates in the preset database into a distributed cache cluster.
[0087] Optionally, the second construction module includes: a first construction submodule, used to construct a first preset template based on template type, sender information, host address and proxy server information; and a second construction submodule, used to construct a second preset template based on template type, recipient information, template content and template title.
[0088] Optionally, the email task to be processed includes at least: task information, and the generation unit 83 includes: a first replacement module, used to replace multiple preset placeholders in the second preset template based on the email content and attachment content in the task information, wherein the email content includes at least: task identifier, title content and body content, and the preset placeholders include at least: preset placeholders for the template title and preset placeholders for the template content; a second replacement module, used to replace the preset placeholders for the template title based on the title content, and replace the preset placeholders for the template content based on the body content, to obtain the replaced second preset template; and a first generation module, used to determine the first preset template corresponding to the replaced second preset template, and generate an email based on the first preset template and the replaced second preset template.
[0089] Optionally, the templated email sending device further includes: a second determining module, used to determine the pending email task corresponding to the email as a second type of email task in the event of email sending failure after the email is sent to the recipient's mailbox, and to re-add the second type of email task to the preset database; and a first releasing module, used to release the resource lock, and after releasing the resource lock, to control all service nodes to re-request the resource lock from the distributed cache cluster in order to process the email task.
[0090] The aforementioned templated email sending device may also include a processor and a memory. The loading unit 80, the acquisition unit 81, the search unit 82, the generation unit 83, etc., are all stored in the memory as program units, and the processor executes the aforementioned program units stored in the memory to realize the corresponding functions.
[0091] The aforementioned processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured; by adjusting kernel parameters, when template information associated with the email task to be processed is found, the kernel generates an email based on the email task and the template information, and then sends the email to the recipient's mailbox.
[0092] The aforementioned memory may include non-permanent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0093] According to another aspect of the present invention, a computer program product is also provided, including a non-volatile computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the method for sending templated emails as described above.
[0094] When a computer program product is executed on a data processing device, it is suitable to execute an initialization program with the following steps: Starting multiple service nodes within the current service cycle; for each service node, loading all template information from a distributed cache cluster and storing all template information in the service node's cache, wherein the distributed cache cluster stores template information for multiple email templates; controlling all service nodes to request resource locks from the distributed cache cluster, and based on the resource locks, determining the target service node, controlling the target service node to query a preset database to obtain a set of email tasks to be processed, wherein the set of email tasks to be processed includes at least: a first type of email task and a second type of email task, the first type of email task being email tasks added within the current service cycle, and the second type of email task being email tasks that failed to be processed in the previous service cycle; for each email task to be processed in the set of email tasks to be processed, searching the cache for template information associated with the email task to be processed; if template information associated with the email task to be processed is found, generating an email based on the email task to be processed and the template information, and sending the email to the recipient's mailbox.
[0095] According to another aspect of the present invention, an electronic device is also provided, including one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the above-described method for sending templated emails.
[0096] Figure 9 This is a hardware structure block diagram of an electronic device (or mobile device) for sending templated emails according to an embodiment of the present invention. Figure 9 As shown, an electronic device may include one or more processors (e.g., Figure 9 The processors 902a, 902b, ..., 902n, etc., may include, but are not limited to, processing devices such as microprocessors (MCUs) or programmable logic devices (FPGAs), and a memory 904 for storing data. In addition, it may include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply, and / or a camera. Those skilled in the art will understand that... Figure 9 The structure shown is for illustrative purposes only and does not limit the structure of the electronic device described above. For example, the electronic device may also include components that are more... Figure 9 The more or fewer components shown, or having the same Figure 9 The different configurations shown.
[0097] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0098] The embodiments or examples disclosed herein are not exhaustive, but merely illustrative of some embodiments or examples, and are not intended to limit the scope of protection of this disclosure. Unless otherwise specified, each step in a particular embodiment or example can be implemented as an independent embodiment, and the steps can be arbitrarily combined. For example, a solution after removing some steps in a particular embodiment or example can also be implemented as an independent embodiment, and the order of the steps in a particular embodiment or example can be arbitrarily interchanged. Furthermore, optional methods or examples in a particular embodiment or example can be arbitrarily combined; moreover, embodiments or examples can be arbitrarily combined. For example, some or all steps of different embodiments or examples can be arbitrarily combined, and a particular embodiment or example can be arbitrarily combined with optional methods or examples of other embodiments or examples.
[0099] In the above embodiments of the present invention, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0100] In the several embodiments provided by this invention, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection can be through some interfaces; the indirect coupling or communication connection of units or modules can be electrical or other forms.
[0101] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0102] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0103] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0104] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for sending templated emails, characterized in that, The method for sending templated emails, applied to an email service system, includes: During the current service cycle, multiple service nodes are started. For each service node, all template information is loaded from the distributed cache cluster and stored in the cache of the service node. The distributed cache cluster stores the template information of multiple email templates. All service nodes are controlled to request resource locks from the distributed cache cluster, and based on the resource locks, a target service node is determined. The target service node is controlled to query a preset database to obtain a set of email tasks to be processed. The set of email tasks to be processed includes at least: a first type of email task and a second type of email task. The first type of email task is an email task added in the current service cycle, and the second type of email task is an email task that failed to be processed in the previous service cycle of the current service cycle. For each pending email task in the set of pending email tasks, retrieve the template information associated with the pending email task from the cache; If the template information associated with the email task to be processed is found, an email is generated based on the email task to be processed and the template information, and the email is sent to the recipient's mailbox.
2. The method for sending templated emails according to claim 1, characterized in that, The steps for determining the target service node based on the resource lock include: If any of the service nodes successfully acquires the resource lock, a preset field is written to the distributed cache cluster, wherein the preset field is used to characterize the owner of the resource lock and the resource lock has a corresponding lifespan; The service node is identified as the target service node.
3. The method for sending templated emails according to claim 1, characterized in that, After determining the target service node based on the resource lock, the process also includes: A monitoring mechanism is constructed, wherein the monitoring mechanism monitors at a preset frequency whether the target service node has finished processing the set of email tasks to be processed; If the target service node has not finished processing the set of pending email tasks, increase the lifetime of the resource lock.
4. The method for sending templated emails according to claim 1, characterized in that, Before retrieving template information associated with the pending email task from the cache, the process also includes: Based on template type and email information, multiple email templates are constructed. Each email template includes at least a first preset template and a second preset template. The first preset template includes at least sender information and network information. The second preset template is an email framework that includes recipient information and the content received by the recipient. The first preset template is associated with multiple second preset templates. Each email template is associated with the email task to be processed through the template type. All the email templates are stored in the preset database, and all the email templates in the preset database are loaded into the distributed cache cluster.
5. The method for sending templated emails according to claim 4, characterized in that, The steps to build multiple email templates based on template type and email information include: Based on the template type, the sender information, the host address, and the proxy server information, the first preset template is constructed; Based on the template type, the recipient information, the template content, and the template title, the second preset template is constructed.
6. The method for sending templated emails according to claim 1, characterized in that, The email task to be processed includes at least: task information; and the step of generating an email based on the email task to be processed and the template information includes: Based on the email content and attachment content in the task information, multiple preset placeholders in the second preset template are replaced. The email content includes at least: task identifier, title content and body content, and the preset placeholders include at least: preset placeholders for template title and preset placeholders for template content. Based on the title content, the preset placeholder of the template title is replaced, and based on the body content, the preset placeholder of the template content is replaced to obtain the replaced second preset template; The first preset template corresponding to the replaced second preset template is determined, and the email is generated based on the first preset template and the replaced second preset template.
7. The method for sending templated emails according to claim 1, characterized in that, After sending the email to the recipient's email address, the following is also included: If the email fails to be sent, the pending email task corresponding to the email is identified as the second type of email task, and the second type of email task is added back to the preset database. Release the resource lock, and after releasing the resource lock, control all the service nodes to re-request the resource lock from the distributed cache cluster in order to process the email task.
8. A templated email sending device, characterized in that, The templated email sending device, used in an email service system, includes: The loading unit is used to start multiple service nodes within the current service cycle. For each service node, it loads all template information from the distributed cache cluster and stores all the template information in the cache of the service node. The distributed cache cluster stores the template information of multiple email templates. The acquisition unit is used to control all the service nodes to apply for resource locks from the distributed cache cluster, and based on the resource locks, determine the target service node, control the target service node to query a preset database, and obtain a set of email tasks to be processed. The set of email tasks to be processed includes at least: a first type of email task and a second type of email task. The first type of email task is an email task added in the current service cycle, and the second type of email task is an email task that failed to be processed in the previous service cycle of the current service cycle. The lookup unit is used to look up template information associated with each email task in the set of email tasks to be processed from the cache. The generation unit is configured to generate an email based on the email task to be processed and the template information when the template information associated with the email task to be processed is found, and send the email to the recipient's mailbox.
9. A computer program product, characterized in that, The method includes a non-volatile computer-readable storage medium storing a computer program that, when executed by a processor, implements the method for sending templated emails as described in any one of claims 1 to 7.
10. An electronic device, characterized in that, The device includes one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the method for sending templated emails as described in any one of claims 1 to 7.