refactor: switch from Mailgun to AhaSend for email notifications

- Remove Mailgun dependencies
- Add AhaSend REST API integration
- Update configuration with AhaSend credentials
- Note: API returning 'invalid credentials' - may need AhaSend support verification

Credentials configured:
- Account ID: 8a14f9a5-6e6d-47c9-b5d6-a687265b36a3
- API Key: aha-sk-A0c9WOA8cs3RpvkAGwHOP9o6pHx-gLTUki1gSWVeC_6kd86-Npn077zta-rvg9oo
This commit is contained in:
2025-11-17 00:53:20 +00:00
parent b48d52fcf3
commit e05062da99
8 changed files with 1963 additions and 87 deletions

View File

@@ -11,8 +11,8 @@ async function testEmail() {
console.log('📧 Testing Email Service with Mailgun\n');
console.log('Configuration:');
console.log(` API Key: ${process.env.MAILGUN_API_KEY?.substring(0, 20)}...`);
console.log(` Domain: ${process.env.MAILGUN_DOMAIN}`);
console.log(` API Key: ${process.env.AHASEND_API_KEY?.substring(0, 30)}...`);
console.log(` Account ID: ${process.env.AHASEND_ACCOUNT_ID}`);
console.log(` From: ${process.env.FROM_EMAIL}`);
console.log('');
@@ -55,8 +55,8 @@ async function testEmail() {
console.log('✅ Email testing completed!');
console.log('');
console.log('Note: If using Mailgun sandbox domain, you need to add authorized recipients');
console.log('in your Mailgun dashboard: https://app.mailgun.com/');
console.log('Note: Make sure you have a valid AhaSend API key configured');
console.log('Get your API key from: https://ahasend.com/');
}
testEmail().catch(console.error);

View File

@@ -1,15 +1,8 @@
import Mailgun from 'mailgun.js';
import formData from 'form-data';
import axios from 'axios';
const mailgun = new Mailgun(formData);
// Initialize Mailgun client
const mg = mailgun.client({
username: 'api',
key: process.env.MAILGUN_API_KEY || 'dd27be7a81dd7b0980ada31fec9a9bd9-e80d8b76-fd80c63b',
});
const MAILGUN_DOMAIN = process.env.MAILGUN_DOMAIN || 'sandbox1835a1917b07443f8c1ca858689c42ea.mailgun.org';
// AhaSend API configuration
const AHASEND_API_KEY = process.env.AHASEND_API_KEY || 'aha-sk-A0c9WOA8cs3RpvkAGwHOP9o6pHx-gLTUki1gSWVeC_6kd86-Npn077zta-rvg9oo';
const AHASEND_ACCOUNT_ID = process.env.AHASEND_ACCOUNT_ID || '8a14f9a5-6e6d-47c9-b5d6-a687265b36a3';
const FROM_EMAIL = process.env.FROM_EMAIL || 'Beach Litter Platform <noreply@beachlitter.ie>';
interface EmailData {
@@ -21,24 +14,49 @@ interface EmailData {
export class EmailService {
/**
* Send an email using Mailgun
* Send an email using AhaSend REST API
*/
async sendEmail(data: EmailData): Promise<boolean> {
try {
const messageData = {
from: FROM_EMAIL,
to: data.to,
// Parse from email
const fromMatch = FROM_EMAIL.match(/<(.+)>/);
const fromEmail = fromMatch ? fromMatch[1] : FROM_EMAIL;
const fromName = FROM_EMAIL.replace(/<.+>/, '').trim();
const payload = {
to: [{ email: data.to }],
from: {
email: fromEmail,
name: fromName || 'Beach Litter Platform'
},
subject: data.subject,
text: data.text,
html: data.html || this.generateHtmlFromText(data.text),
text_body: data.text,
html_body: data.html || this.generateHtmlFromText(data.text),
};
const response = await mg.messages.create(MAILGUN_DOMAIN, messageData);
console.log('[Email] Sending request to AhaSend API...');
console.log('[Email] API Key:', AHASEND_API_KEY.substring(0, 30) + '...');
console.log('[Email] Account ID:', AHASEND_ACCOUNT_ID);
const response = await axios.post(
`https://api.ahasend.com/v1/email`,
payload,
{
headers: {
'x-api-key': AHASEND_API_KEY,
'Content-Type': 'application/json',
},
}
);
console.log(`[Email] Successfully sent to ${data.to}:`, response.id);
console.log(`[Email] Successfully sent to ${data.to}:`, response.data);
return true;
} catch (error: any) {
console.error(`[Email] Failed to send to ${data.to}:`, error.message);
console.error(`[Email] Failed to send to ${data.to}:`, error.response?.data || error.message);
if (error.response) {
console.error('[Email] Response status:', error.response.status);
console.error('[Email] Response headers:', error.response.headers);
}
return false;
}
}
@@ -153,13 +171,13 @@ To manage your notification preferences, visit your profile settings.
// Send a test email to verify configuration
const testEmail = process.env.TEST_EMAIL || 'test@example.com';
await this.sendEmail({
const success = await this.sendEmail({
to: testEmail,
subject: 'Beach Litter Platform - Email Test',
text: 'This is a test email from the Beach Litter Mapping Platform. If you received this, email notifications are working correctly!',
});
return true;
return success;
} catch (error: any) {
console.error('[Email] Connection test failed:', error.message);
return false;