const AWS = require('aws-sdk')
require('dotenv').config()

const awsConfig = {
    region: process.env.AWS_REGION,
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}

AWS.config.update(awsConfig);

async function sendSimpleMail(from, to, subject, text) {

    const params = {
        Destination: { /* required */
            CcAddresses: [],
            ToAddresses: [
                to
            ]
        },
        Message: { /* required */
            Body: { /* required */
                Html: {
                    Charset: "UTF-8",
                    Data: text
                }
            },
            Subject: {
                Charset: 'UTF-8',
                Data: subject
            }
        },
        Source: from,
        ReplyToAddresses: [
            from
        ],
    }

    // Create the promise and SES service object
    const sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
    try {
        const sendResult =  await sendPromise
        console.log('sendSimpleMail sent', params, sendResult)
        return sendResult
    } catch (e) {
        console.log('sendSimpleMail error', e)
    }
}

module.exports = {
    sendSimpleMail
}