/** * Microsoft 365: odeslani e-mailu pres Graph. * * Sluzba: https://services.csbot.cz/apps/microsoft-365-service * Endpoint: POST /users/{user_id}/mail/send * * Napojeni je **aplikacni**, ne uzivatelske: prihlasuje se registrovana * aplikace, ne clovek. Proto je schranka vzdy soucasti kroku - aplikace sama * o sobe zadnou nema a bez uvedeneho uzivatele by Graph nevedel, odkud poslat. */ export const manifest = { id: 'microsoft365.send-mail', name: 'Odeslat e-mail', description: 'Odešle e-mail ze zvolené schránky Microsoft 365. Příjemců může být víc, ' + 'oddělují se čárkou.', inputs: [ { id: 'mailbox', label: 'Schránka odesílatele', type: 'string', required: true, hint: 'E-mail nebo ID uživatele, například servis@firma.cz.', }, { id: 'to', label: 'Příjemci', type: 'string', required: true, hint: 'E-maily oddělené čárkou.', }, { id: 'cc', label: 'Kopie', type: 'string', required: false }, { id: 'subject', label: 'Předmět', type: 'string', required: true }, { id: 'body', label: 'Text zprávy', type: 'string', required: true, multiline: true, }, { id: 'html', label: 'Text je HTML', type: 'boolean', required: false, default: true, hint: 'Vypnuté odešle zprávu jako čistý text.', }, { id: 'replyTo', label: 'Adresa pro odpověď', type: 'string', required: false, hint: 'Když se má odpovídat jinam než do schránky odesílatele.', }, ], outputs: [ { id: 'sent', label: 'Odesláno', type: 'boolean', required: true }, { id: 'recipients', label: 'Počet příjemců', type: 'number', required: true }, { id: 'subject', label: 'Předmět', type: 'string', required: true }, ], timeoutMs: 30000, }; /** Seznam adres z jednoho radku. Graph chce pole objektu, clovek pise carky. */ function addresses(value) { return String(value ?? '') .split(/[,;]/) .map((item) => item.trim()) .filter((item) => item !== '') .map((address) => ({ address })); } export async function run(inputs, ctx) { const to = addresses(inputs.to); if (to.length === 0) ctx.fail('Nezůstal žádný příjemce, zkontrolujte pole Příjemci.'); const payload = { subject: inputs.subject, body: inputs.body, body_content_type: inputs.html ? 'HTML' : 'Text', to, }; const cc = addresses(inputs.cc); if (cc.length > 0) payload.cc = cc; if (inputs.replyTo) payload.reply_to = addresses(inputs.replyTo); // Cesta obsahuje e-mail, ktery muze mit znaky mimo ASCII. await ctx.http.post(`/users/${encodeURIComponent(inputs.mailbox)}/mail/send`, payload); ctx.log(`Odesláno ${to.length + cc.length} příjemcům ze schránky ${inputs.mailbox}.`); // Graph na uspesne odeslani nevraci telo, vystup se sklada ze vstupu. return { sent: true, recipients: to.length + cc.length, subject: inputs.subject }; }