import nodemailer from 'nodemailer'; import moment from 'moment'; import type { RowDataPacket } from 'mysql2'; import pool from './mysql'; import { checkAblauf } from './checkAblauf'; // SMTP-Konfiguration via Umgebungsvariablen (Passwort niemals hart codieren!) const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST || 'smtp.1und1.de', port: parseInt(process.env.SMTP_PORT || '587', 10), secure: false, auth: { user: process.env.SMTP_USER || '', pass: process.env.SMTP_PASS || '', }, }); export async function doCheckAndMail(): Promise { const [rows] = await pool.query( 'SELECT tab, pday, cnt, at FROM tabletten' ); let body = ''; for (const item of rows) { if (item.pday !== 0) { const updates = checkAblauf({ cnt: item.cnt, pday: item.pday, at: item.at }); await pool.execute( 'UPDATE tabletten SET akt = ?, until = ?, warn = ? WHERE tab = ?', [updates.akt, moment(updates.until).format('YYYY-MM-DD'), updates.warn ? 1 : 0, item.tab] ); if (updates.warn) { const name = item.tab.split(' ')[0]; body += `"${name}" wird am ${moment(updates.until).format('YYYY-MM-DD')} (in ${updates.rtage} Tagen) zu Ende sein\n`; } } } if (body) { await transporter.sendMail({ from: `"Tabletten" <${process.env.SMTP_USER}>`, to: process.env.MAIL_TO || '', subject: 'Tabletten gehen zu Ende', text: body, }); } return body || 'Kein Warn-Eintrag.'; }