Я предоставлю вам различные способы отправки уведомлений по электронной почте с настраиваемым шаблоном представления на разных языках программирования. Обратите внимание, что приведенные примеры кода предназначены для демонстрационных целей и могут потребовать внесения изменений в зависимости от вашего конкретного случая использования. Вот несколько методов:
-
Python (с использованием Flask и Flask-Mail):
from flask import Flask, render_template from flask_mail import Mail, Message app = Flask(__name__) app.config['MAIL_SERVER'] = 'your-mail-server' app.config['MAIL_PORT'] = 587 app.config['MAIL_USE_TLS'] = True app.config['MAIL_USERNAME'] = 'your-username' app.config['MAIL_PASSWORD'] = 'your-password' mail = Mail(app) @app.route('/send_email') def send_email(): recipient = 'recipient@example.com' subject = 'Notification Email' template = 'custom_email_template.html' # Render the template with any required data email_body = render_template(template, data=data) msg = Message(subject, sender='sender@example.com', recipients=[recipient]) msg.html = email_body mail.send(msg) return 'Email sent!' if __name__ == '__main__': app.run() -
Ruby on Rails (с использованием Action Mailer):
# app/mailers/notification_mailer.rb class NotificationMailer < ApplicationMailer def send_notification_email(recipient, data) @data = data mail(to: recipient, subject: 'Notification Email') do |format| format.html { render 'custom_email_template' } end end end # app/views/notification_mailer/custom_email_template.html.erb <!-- Your HTML template here --> # app/controllers/notification_controller.rb class NotificationController < ApplicationController def send_email recipient = 'recipient@example.com' data = { key: 'value' } NotificationMailer.send_notification_email(recipient, data).deliver_now render plain: 'Email sent!' end end # config/routes.rb Rails.application.routes.draw do get '/send_email', to: 'notification#send_email' end -
PHP (с использованием PHPMailer):
require 'PHPMailer/PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = 'your-mail-server'; $mail->Port = 587; $mail->SMTPAuth = true; $mail->Username = 'your-username'; $mail->Password = 'your-password'; $mail->SMTPSecure = 'tls'; $mail->setFrom('sender@example.com', 'Sender Name'); $mail->addAddress('recipient@example.com', 'Recipient Name'); $mail->isHTML(true); $mail->Subject = 'Notification Email'; $template = file_get_contents('custom_email_template.html'); // Replace placeholders in the template with actual values $template = str_replace('{{key}}', $value, $template); $mail->Body = $template; if ($mail->send()) { echo 'Email sent!'; } else { echo 'Error: ' . $mail->ErrorInfo; } -
Node.js (с использованием Nodemailer):
const nodemailer = require('nodemailer'); const fs = require('fs'); const ejs = require('ejs'); const transporter = nodemailer.createTransport({ host: 'your-mail-server', port: 587, secure: false, auth: { user: 'your-username', pass: 'your-password' } }); const recipient = 'recipient@example.com'; const subject = 'Notification Email'; const template = fs.readFileSync('custom_email_template.ejs', 'utf-8'); const data = { key: 'value' }; const emailBody = ejs.render(template, data); const mailOptions = { from: 'sender@example.com', to: recipient, subject: subject, html: emailBody }; transporter.sendMail(mailOptions, (error, info) => { if (error) { console.log('Error: ', error); } else { console.log('Email sent!'); } });
Это всего лишь несколько примеров того, как можно отправить электронное письмо с уведомлением с помощью настраиваемого шаблона представления на разных языках программирования. Не забудьте настроить код в соответствии со своими требованиями.