lowscarlet
HOBBYOP
6 months ago
I want to send email messages from my web system for notifications, but railway.com blocks SMTP for the free and hobby plans. Since my web system is still under development, I can’t upgrade to the pro plan yet.
So, I followed the suggestion to use the Gmail API without SMTP. Here’s an example:
// setup OAuth2 client
const oAuth2Client = new google.auth.OAuth2(
BOT_GOOGLE.id,
BOT_GOOGLE.secret,
"https://developers.google.com/oauthplayground"
);
// inject refresh token
oAuth2Client.setCredentials({
refresh_token: BOT_EMAIL.refreshToken
});
// create transporter with OAuth2
async function createTransporter() {
const accessToken = await oAuth2Client.getAccessToken();
return nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: BOT_EMAIL.user,
clientId: BOT_GOOGLE.id,
clientSecret: BOT_GOOGLE.secret,
refreshToken: BOT_EMAIL.refreshToken,
accessToken: accessToken.token ?? "",
},
});
}It works fine when I test locally, but it doesn’t work on Railway.com.