emoleumassi
emoleumassi

Reputation: 5129

why amplify didn't send ses email

I created a script in node.js to send email with aws-ses.

My script:

import express from 'express';

const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" });
const app: express.Application = express();
const bodyParser = require('body-parser');
const port = 3000;
const path = require('path');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, '..', 'public')));

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
});

// @ts-ignore
app.post('/', (req, res) => {
    send(req.body.name, req.body.address, req.body.text)
});
app.listen(port, () => {
    console.log(`Villa app listening on port ${port}!`);
});

// @ts-ignore
function send(name, address, text) {

    // Create sendEmail params
    let params = {
        Destination: {
            /* required */
            CcAddresses: [
                "xxxx",
                /* more items */
            ],
            ToAddresses: [
                address,
                /* more items */
            ],
        },
        Message: {
            /* required */
            Body: {
                /* required */
                Html: {
                    Charset: "UTF-8",
                    Data: text,
                },
                Text: {
                    Charset: "UTF-8",
                    Data: text,
                },
            },
            Subject: {
                Charset: "UTF-8",
                Data: "Nouveau Message de la Residence Bloc H",
            },
        },
        Source: "xxx" /* required */,
        ReplyToAddresses: [
            "xxx",
            /* more items */
        ],
    };

    // Create the promise and SES service object
    let sendPromise = new AWS.SES({ apiVersion: "2010-12-01" })
        .sendEmail(params)
        .promise();

    // Handle promise's fulfilled/rejected states
    sendPromise
        // @ts-ignore
        .then(function (data) {
            console.log(data.MessageId);
        })
        // @ts-ignore
        .catch(function (err) {
            console.error(err, err.stack);
        });
}

Locally it work's because i created a user with this policies AmazonSesSendingAccess and saved the security credentials on my computer.

I created an app with amplify and added my code.

I created a role with IAM with those two policies: AdministratorAccess-Amplify and AmazonSESFullAccess and added this roles as service role to my amplify app.

But into the amplify app on the aws cloud i can't send the email.

Upvotes: 0

Views: 25

Answers (0)

Related Questions