www/hooks/email-customer.js

21 lines
1.8 KiB
JavaScript

'use strict'
require('dotenv').config()
const formatUSD = require('../lib/formatUSD')
const mailer = require('../lib/mailer')
module.exports = async (order) => {
const customer_mail_items_string = await order.items.reduce((acc,cur) =>
acc += `${cur.qty} ${formatUSD(cur.price)} ${cur.subtotal} ${cur.name}\n`
, 'Qty Price Subtotal Item\n')
let customer_mail_res; try {
console.log(`Emailing order ${order.id} confirmation to customer at ${order.contact.email}`)
customer_mail_res = await mailer.sendMail({
from: process.env.SALES_MAIL_FROM,
to: order.contact.email,
replyTo: process.env.SALES_EMAIL,
subject: `Order ${order.id} for ${formatUSD(order.total)} has been processed`,
text: `Dear ${order.contact.name},\n\nThank you for your payment. We will ship your order as soon as possible and send the tracking info to this email address. For now, keep this copy of your order details as proof of your payment. \n\n---\n\nITEMS: \n${customer_mail_items_string}\nTOTALS: \nSubtotal: ${formatUSD(order.subtotal)}\nTax: ${formatUSD(order.taxAmount)}\nShipping: ${formatUSD(order.shipping.amount)}\nProcessing: ${formatUSD(order.processing)}\n\nCONTACT: \n${order.contact.name}\n${order.contact.phone}\n${order.contact.email}\n\nSHIPPING ADDRESS:\n${order.shipping.address.name}\n${order.shipping.address.addr1}${(order.shipping.address.addr2)?'\n'+order.shipping.address.addr2:''}\n${order.shipping.address.city}, ${order.shipping.address.state} ${order.shipping.address.zip}\n\n---\n\nTo view your order info anytime, visit this page: ${process.env.SITE_URL}/shop/order/stripe/?order=${order.id}\n\nFor questions, comments, or to cancel this order, reply to this email or call 719-936-7778 x1. \n\nThank you!\n\n`,
})
console.log(`Sent email ${customer_mail_res.messageId} to customer for order ${order.id}`)
} catch (err) { console.error(err) }
}