I have been struggling for a while trying to find a solution for a back end where the user can provide and email, name and message form angular side and I would send the email with something like firebase functions…
I tried using the SendGrid and nodeMailer but I am unable to send the email from the clients email ex: [email protected]
If you can provide an example that would be great.
Here is what I have been struggling with, I am bale to send it from my emails but not on behalf of the client.
import * as functions from "firebase-functions";
const sgMail = require("@sendgrid/mail");
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const cors = require('cors');
cors({
origin: "*"
})
exports.emailMessage = functions.https.onCall((data) => {
sgMail.setApiKey("mykey");
const msg = {
to: '[email protected]',
from: data.sender,
subject: `${data.name} sent you email`,
text: data.message,
html: `<strong>${data.message}</strong>`,
}
sgMail
.send(msg)
.then(() => {
console.log('Email sent');
})
.catch((error:any) => {
console.error(error);
})
return{"status": "sucess", data: data};
});
exports.nodeEmailMessage = functions.https.onCall((data) => {
const asccessKey = '[email protected]';
const secretKey = 'my oassword';
const transporter = nodemailer.createTransport(smtpTransport({
host: "smtp-mail.outlook.com",
secureConnection: false, // TLS requires secureConnection to be false
port: 587, // port for secure SMTP
tls: {
ciphers:'SSLv3'
},
auth: {
user: asccessKey,
pass: secretKey
}}));
transporter.verify(function (error:any, success:any) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
const mailOptions = {
to: asccessKey,
from: data.sender,
subject: `${data.name} sent you a new message`,
text: data.message,
html: data.message
};
transporter.sendMail(mailOptions, function(error:any, info:any){
if(error){
console.log(error.message);
}
});
return {"sucsess": data};
});
