codenuk_backend_mine/services/user-auth/test-email.js

76 lines
2.5 KiB
JavaScript

#!/usr/bin/env node
require('dotenv').config();
const { sendMail } = require('./src/utils/email');
async function testEmail() {
console.log('🧪 Testing Email Configuration');
console.log('================================');
// Check environment variables
console.log('\n🔧 Environment Variables:');
console.log('NODE_ENV:', process.env.NODE_ENV || 'development');
console.log('SMTP_HOST:', process.env.SMTP_HOST || 'Not set');
console.log('SMTP_USER:', process.env.SMTP_USER || 'Not set');
console.log('SMTP_PASS:', process.env.SMTP_PASS ? '***Set***' : 'Not set');
console.log('GMAIL_USER:', process.env.GMAIL_USER || 'Not set');
console.log('GMAIL_APP_PASSWORD:', process.env.GMAIL_APP_PASSWORD ? '***Set***' : 'Not set');
console.log('SMTP_FROM:', process.env.SMTP_FROM || 'Not set');
// Test email sending
console.log('\n📧 Testing Email Sending...');
const testEmail = {
to: 'test@example.com',
subject: 'Test Email - User Auth Service',
text: 'This is a test email to verify email configuration.',
html: `
<div style="font-family: Arial, sans-serif; padding: 20px;">
<h2>🧪 Test Email</h2>
<p>This is a test email to verify email configuration for the User Auth Service.</p>
<p><strong>Timestamp:</strong> ${new Date().toISOString()}</p>
<p><strong>Environment:</strong> ${process.env.NODE_ENV || 'development'}</p>
<hr>
<p style="color: #666; font-size: 12px;">
If you receive this email, your email configuration is working correctly!
</p>
</div>
`
};
try {
const result = await sendMail(
testEmail.to,
testEmail.subject,
testEmail.text,
testEmail.html
);
console.log('✅ Email test successful!');
console.log('Message ID:', result.messageId);
console.log('Response:', result.response);
} catch (error) {
console.error('❌ Email test failed:');
console.error('Error:', error.message);
if (error.code) {
console.error('Error Code:', error.code);
}
if (error.response) {
console.error('SMTP Response:', error.response);
}
console.error('\n🔧 Troubleshooting Tips:');
console.error('1. Check your email credentials');
console.error('2. Verify 2FA is enabled for Gmail');
console.error('3. Use App Password, not regular password');
console.error('4. Check firewall/network settings');
console.error('5. Verify SMTP port (587 for Gmail)');
}
}
// Run the test
testEmail().catch(console.error);