Beispiele für Lambda-Funktionen - Amazon Simple Email Service

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Beispiele für Lambda-Funktionen

Dieses Thema enthält Beispiele für Lambda-Funktionen, die den Nachrichtenfluss kontrollieren.

Beispiel 1: Löschen von Spam

Dieses Beispiel stoppt die Verarbeitung von Nachrichten mit mindestens einem Spam-Indikator.

exports.handler = function(event, context, callback) { console.log('Spam filter'); var sesNotification = event.Records[0].ses; console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2)); // Check if any spam check failed if (sesNotification.receipt.spfVerdict.status === 'FAIL' || sesNotification.receipt.dkimVerdict.status === 'FAIL' || sesNotification.receipt.spamVerdict.status === 'FAIL' || sesNotification.receipt.virusVerdict.status === 'FAIL') { console.log('Dropping spam'); // Stop processing rule set, dropping message callback(null, {'disposition':'STOP_RULE_SET'}); } else { callback(null, null); } };

Beispiel 2: Fortsetzen, wenn ein bestimmter Header gefunden wird

In diesem Beispiel wird die Verarbeitung der aktuellen Regel nur dann fortgesetzt, wenn die E-Mail einen bestimmten Header-Wert enthält.

exports.handler = function(event, context, callback) { console.log('Header matcher'); var sesNotification = event.Records[0].ses; console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2)); // Iterate over the headers for (var index in sesNotification.mail.headers) { var header = sesNotification.mail.headers[index]; // Examine the header values if (header.name === 'X-Header' && header.value === 'X-Value') { console.log('Found header with value.'); callback(null, null); return; } } // Stop processing the rule if the header value wasn't found callback(null, {'disposition':'STOP_RULE'}); };

Beispiel 3: Abrufen von E-Mail aus Amazon S3

In diesem Beispiel wird die Raw-E-Mail aus Amazon S3 abgerufen und verarbeitet.

Anmerkung

Sie müssen zunächst die E-Mail mit einer S3-Aktion in Amazon S3 schreiben.

var AWS = require('aws-sdk'); var s3 = new AWS.S3(); var bucketName = '<YOUR BUCKET GOES HERE>'; exports.handler = function(event, context, callback) { console.log('Process email'); var sesNotification = event.Records[0].ses; console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2)); // Retrieve the email from your bucket s3.getObject({ Bucket: bucketName, Key: sesNotification.mail.messageId }, function(err, data) { if (err) { console.log(err, err.stack); callback(err); } else { console.log("Raw email:\n" + data.Body); // Custom email processing goes here callback(null, null); } }); };

Beispiel 4: Unzustellbarkeit von Nachrichten, bei denen die DMARC-Authentifizierung fehlschlägt

In diesem Beispiel wird eine Unzustellbarkeitsnachricht gesendet, wenn die DMARC-Authentifizierung einer eingehenden E-Mail fehlschlägt.

Anmerkung

Legen Sie bei der Verwendung dieses Beispiels den Wert der Umgebungsvariablen emailDomain auf Ihre Domäne für den E-Mail-Empfang fest.

'use strict'; const AWS = require('aws-sdk'); // Assign the emailDomain environment variable to a constant. const emailDomain = process.env.emailDomain; exports.handler = (event, context, callback) => { console.log('Spam filter starting'); const sesNotification = event.Records[0].ses; const messageId = sesNotification.mail.messageId; const receipt = sesNotification.receipt; console.log('Processing message:', messageId); // If DMARC verdict is FAIL and the sending domain's policy is REJECT // (p=reject), bounce the email. if (receipt.dmarcVerdict.status === 'FAIL' && receipt.dmarcPolicy.status === 'REJECT') { // The values that make up the body of the bounce message. const sendBounceParams = { BounceSender: `mailer-daemon@${emailDomain}`, OriginalMessageId: messageId, MessageDsn: { ReportingMta: `dns; ${emailDomain}`, ArrivalDate: new Date(), ExtensionFields: [], }, // Include custom text explaining why the email was bounced. Explanation: "Unauthenticated email is not accepted due to the sending domain's DMARC policy.", BouncedRecipientInfoList: receipt.recipients.map((recipient) => ({ Recipient: recipient, // Bounce with 550 5.6.1 Message content rejected BounceType: 'ContentRejected', })), }; console.log('Bouncing message with parameters:'); console.log(JSON.stringify(sendBounceParams, null, 2)); // Try to send the bounce. new AWS.SES().sendBounce(sendBounceParams, (err, data) => { // If something goes wrong, log the issue. if (err) { console.log(`An error occurred while sending bounce for message: ${messageId}`, err); callback(err); // Otherwise, log the message ID for the bounce email. } else { console.log(`Bounce for message ${messageId} sent, bounce message ID: ${data.MessageId}`); // Stop processing additional receipt rules in the rule set. callback(null, { disposition: 'stop_rule_set', }); } }); // If the DMARC verdict is anything else (PASS, QUARANTINE or GRAY), accept // the message and process remaining receipt rules in the rule set. } else { console.log('Accepting message:', messageId); callback(); } };