|
- import express from 'express'
- import cors from 'cors'
- import bodyParser from 'body-parser'
- import amqp from 'amqplib'
- // import nodemailer from 'nodemailer'
- import SibApiV3Sdk from 'sib-api-v3-sdk'
- import dotenv from 'dotenv'
- dotenv.config()
-
- const app = express()
- const queueName = process.env.QUEUE_NAME
- const senderEmail = process.env.SENDER_EMAIL
- // const mailTransporter = nodemailer.createTransport({
- // service: 'gmail',
- // auth: {
- // user: process.env.TRANS_NAME,
- // pass: process.env.TRANS_PASS
- // }
- // })
- var defaultClient = SibApiV3Sdk.ApiClient.instance
- var apiKey = defaultClient.authentications['api-key']
- apiKey.apiKey = process.env.SENDING_BLUE_API_KEY
- var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
- var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail()
-
- app.use(cors())
- app.use(bodyParser.json())
-
- app.get('/', (_req, res) => {
- res.status.send(200).send("Amigo Receipt Sender Service Homepage!")
- })
-
- amqp.connect('amqp://localhost' ).then(async conn=> {
- const ch = await conn.createChannel()
- const queue = ch.assertQueue(queueName, { durable: true })
- if (queue) {
- queue.then(() => {
- return ch.consume(queueName, async (msg) => {
- var messageBody = JSON.parse(msg.content.toString())
- console.log(`[*] Message Received! email : ${messageBody.email} and pesan : ${messageBody.html}`)
- sendReceipt(messageBody.email, messageBody.html)
- }, { noAck: true })
- }).then(() => {
- console.log('* Waiting for messages from queue ' + queueName)
- })
- }
- }).catch(console.warn)
-
- function sendReceipt(recepientEmail, receipt) {
- var date = new Date()
- sendSmtpEmail = {
- sender: {email: senderEmail, name: 'ReceiptSenderAutomation'},
- to: [{email: recepientEmail, name: 'Customer'}],
- subject: "test",
- textContent: "testing",
- htmlContent: receipt
- }
- apiInstance.sendTransacEmail(sendSmtpEmail).then(function(data) {
- console.log('API called successfully. Returned data: ' + data)
- console.log(date.toLocaleString())
- }, function(error) {
- console.error(error)
- })
-
- // ============ GMAIL's ============
- // var nodemailerOptions = {
- // from: process.env.SENDER_EMAIL,
- // to: receiverEmail,
- // subject: "Nota " + date.toLocaleDateString(),
- // text: receiptToSend
- // }
- // mailTransporter.sendMail(nodemailerOptions, function(err, info) {
- // if (err) {
- // console.log(err.message)
- // }
- // if (info) {
- // console.log(`Email sent!` + " | " +today.toLocaleTimeString("id"))
- // }
- // })
- }
-
- // sendReceipt("michael.pandu270@gmail.com", "Berikut detail nota terbaru anda")
|