BelikanM commited on
Commit
352d951
·
1 Parent(s): 83ea030

Switch to SendGrid API for email sending (SMTP blocked on HF Spaces)

Browse files
Files changed (2) hide show
  1. package.json +1 -0
  2. server.js +17 -25
package.json CHANGED
@@ -7,6 +7,7 @@
7
  "start": "node server.js"
8
  },
9
  "dependencies": {
 
10
  "cloudinary": "^1.41.0",
11
  "cors": "^2.8.5",
12
  "dotenv": "^16.3.1",
 
7
  "start": "node server.js"
8
  },
9
  "dependencies": {
10
+ "@sendgrid/mail": "^8.1.3",
11
  "cloudinary": "^1.41.0",
12
  "cors": "^2.8.5",
13
  "dotenv": "^16.3.1",
server.js CHANGED
@@ -4,6 +4,7 @@ const crypto = require('crypto');
4
  const cors = require('cors');
5
  const https = require('https');
6
  const cloudinary = require('cloudinary').v2;
 
7
  require('dotenv').config({ path: process.cwd() + '/.env' });
8
 
9
  const app = express();
@@ -19,18 +20,9 @@ cloudinary.config({
19
 
20
  console.log('Cloudinary configured with cloud_name:', process.env.CLOUDINARY_CLOUD_NAME);
21
 
22
- // Configuration Nodemailer
23
- const transporter = nodemailer.createTransport({
24
- host: 'smtp.gmail.com',
25
- port: 465,
26
- secure: true, // true for 465, false for 587
27
- auth: {
28
- user: process.env.EMAIL_USER,
29
- pass: process.env.EMAIL_PASS
30
- }
31
- });
32
-
33
- console.log('Nodemailer transporter created for user:', process.env.EMAIL_USER);
34
 
35
  // Stockage temporaire des OTP
36
  const otpStore = new Map();
@@ -111,13 +103,13 @@ app.post('/register', async (req, res) => {
111
  otpStore.set(email, { otp, expires: Date.now() + 5 * 60 * 1000 });
112
 
113
  try {
114
- console.log('Sending OTP email to:', email);
115
- await transporter.sendMail({
116
- from: process.env.EMAIL_USER,
117
  to: email,
 
118
  subject: 'Code de vérification - Inscription',
119
  text: `Votre code de vérification est: ${otp}`
120
- });
 
121
  console.log('OTP email sent successfully for registration');
122
  res.json({ message: 'Code envoyé à votre email' });
123
  } catch (error) {
@@ -143,13 +135,13 @@ app.post('/login', async (req, res) => {
143
  otpStore.set(email, { otp, expires: Date.now() + 5 * 60 * 1000 });
144
 
145
  try {
146
- console.log('Sending login OTP email to:', email);
147
- await transporter.sendMail({
148
- from: process.env.EMAIL_USER,
149
  to: email,
 
150
  subject: 'Code de vérification - Connexion',
151
  text: `Votre code de vérification est: ${otp}`
152
- });
 
153
  console.log('Login OTP email sent successfully');
154
  res.json({ message: 'Code envoyé à votre email' });
155
  } catch (error) {
@@ -198,20 +190,20 @@ app.post('/send-notification', async (req, res) => {
198
  }
199
 
200
  // Configuration de l'email
201
- const mailOptions = {
202
- from: process.env.EMAIL_USER,
203
  to: to,
 
204
  subject: subject,
205
  html: html
206
  };
207
 
208
  // Envoi de l'email
209
- const info = await transporter.sendMail(mailOptions);
210
- console.log('Email de notification envoyé:', info.messageId);
211
 
212
  res.json({
213
  message: 'Notification envoyée avec succès',
214
- messageId: info.messageId,
215
  action_type,
216
  entity_type,
217
  entity_id,
 
4
  const cors = require('cors');
5
  const https = require('https');
6
  const cloudinary = require('cloudinary').v2;
7
+ const sgMail = require('@sendgrid/mail');
8
  require('dotenv').config({ path: process.cwd() + '/.env' });
9
 
10
  const app = express();
 
20
 
21
  console.log('Cloudinary configured with cloud_name:', process.env.CLOUDINARY_CLOUD_NAME);
22
 
23
+ // Configuration SendGrid
24
+ sgMail.setApiKey(process.env.SENDGRID_API_KEY);
25
+ console.log('SendGrid configured');
 
 
 
 
 
 
 
 
 
26
 
27
  // Stockage temporaire des OTP
28
  const otpStore = new Map();
 
103
  otpStore.set(email, { otp, expires: Date.now() + 5 * 60 * 1000 });
104
 
105
  try {
106
+ const msg = {
 
 
107
  to: email,
108
+ from: process.env.EMAIL_USER,
109
  subject: 'Code de vérification - Inscription',
110
  text: `Votre code de vérification est: ${otp}`
111
+ };
112
+ await sgMail.send(msg);
113
  console.log('OTP email sent successfully for registration');
114
  res.json({ message: 'Code envoyé à votre email' });
115
  } catch (error) {
 
135
  otpStore.set(email, { otp, expires: Date.now() + 5 * 60 * 1000 });
136
 
137
  try {
138
+ const msg = {
 
 
139
  to: email,
140
+ from: process.env.EMAIL_USER,
141
  subject: 'Code de vérification - Connexion',
142
  text: `Votre code de vérification est: ${otp}`
143
+ };
144
+ await sgMail.send(msg);
145
  console.log('Login OTP email sent successfully');
146
  res.json({ message: 'Code envoyé à votre email' });
147
  } catch (error) {
 
190
  }
191
 
192
  // Configuration de l'email
193
+ const msg = {
 
194
  to: to,
195
+ from: process.env.EMAIL_USER,
196
  subject: subject,
197
  html: html
198
  };
199
 
200
  // Envoi de l'email
201
+ const result = await sgMail.send(msg);
202
+ console.log('Email de notification envoyé:', result[0].statusCode);
203
 
204
  res.json({
205
  message: 'Notification envoyée avec succès',
206
+ statusCode: result[0].statusCode,
207
  action_type,
208
  entity_type,
209
  entity_id,