diff --git a/influence/app/controllers/campaigns.js b/influence/app/controllers/campaigns.js index 985b69c..f3c9260 100644 --- a/influence/app/controllers/campaigns.js +++ b/influence/app/controllers/campaigns.js @@ -532,6 +532,7 @@ class CampaignsController { // Send email if SMTP method if (emailMethod === 'smtp') { + console.log('DEBUG: About to send campaign email...'); emailResult = await emailService.sendCampaignEmail( recipientEmail, userEmail, @@ -543,9 +544,11 @@ class CampaignsController { recipientName, recipientLevel ); + console.log('DEBUG: Campaign email service returned:', emailResult); } // Log the campaign email + console.log('DEBUG: About to log campaign email with emailResult:', emailResult); await nocoDB.logCampaignEmail({ campaign_id: campaign.ID || campaign.Id || campaign.id, campaign_slug: slug, @@ -561,6 +564,7 @@ class CampaignsController { message: message, status: emailMethod === 'mailto' ? 'clicked' : (emailResult.success ? 'sent' : 'failed') }); + console.log('DEBUG: Campaign email logged with status:', emailMethod === 'mailto' ? 'clicked' : (emailResult.success ? 'sent' : 'failed')); if (emailMethod === 'smtp') { if (emailResult.success) { diff --git a/influence/app/services/email.js b/influence/app/services/email.js index 3333c9f..1c5fabd 100644 --- a/influence/app/services/email.js +++ b/influence/app/services/email.js @@ -92,28 +92,40 @@ class EmailService { }); } + console.log('DEBUG: About to send email via SMTP...'); const info = await this.transporter.sendMail(mailOptions); - console.log('Email sent successfully:', info.messageId); + console.log('DEBUG: Email sent via SMTP successfully:', info.messageId); + console.log('DEBUG: Email info response:', info.response); // Log email to database if NocoDB service is available - await this.logEmailSent({ - to: emailOptions.to, // Log original recipient - subject: emailOptions.subject, // Log original subject - status: 'sent', - messageId: info.messageId, - testMode: testMode, - senderName: emailOptions.from?.name || 'System', - senderEmail: emailOptions.from?.email || process.env.SMTP_FROM_EMAIL - }); + console.log('DEBUG: About to log email to database...'); + try { + await this.logEmailSent({ + to: emailOptions.to, // Log original recipient + subject: emailOptions.subject, // Log original subject + status: 'sent', + messageId: info.messageId, + testMode: testMode, + senderName: emailOptions.from?.name || 'System', + senderEmail: emailOptions.from?.email || process.env.SMTP_FROM_EMAIL + }); + console.log('DEBUG: Successfully logged email to database'); + } catch (logError) { + console.error('DEBUG: Failed to log email to database:', logError); + // Continue anyway - don't let logging failure affect email success + } - return { + const successResult = { success: true, messageId: info.messageId, response: info.response, testMode: testMode, originalRecipient: testMode ? emailOptions.to : undefined }; + + console.log('DEBUG: Returning success result:', successResult); + return successResult; } catch (error) { console.error('Email send error:', error); @@ -218,9 +230,12 @@ class EmailService { // Template-based email methods async sendTemplatedEmail(templateName, templateVariables, emailOptions, isTest = false) { + console.log('DEBUG: sendTemplatedEmail called with template:', templateName); try { // Render the template + console.log('DEBUG: About to render template...'); const { html, text } = await emailTemplates.render(templateName, templateVariables); + console.log('DEBUG: Template rendered successfully'); // Prepare email options with rendered content const mailOptions = { @@ -230,9 +245,12 @@ class EmailService { }; // Send the email using existing sendEmail method - return await this.sendEmail(mailOptions, isTest); + console.log('DEBUG: About to call sendEmail from sendTemplatedEmail...'); + const result = await this.sendEmail(mailOptions, isTest); + console.log('DEBUG: sendEmail returned result:', result); + return result; } catch (error) { - console.error('Failed to send templated email:', error); + console.error('DEBUG: Failed to send templated email:', error); return { success: false, error: error.message @@ -266,6 +284,7 @@ class EmailService { } async sendCampaignEmail(recipientEmail, userEmail, userName, postalCode, subject, message, campaignTitle, recipientName = null, recipientLevel = null) { + console.log('DEBUG: sendCampaignEmail called for recipient:', recipientEmail); const templateVariables = { MESSAGE: message, USER_NAME: userName, @@ -286,7 +305,10 @@ class EmailService { subject: subject }; - return await this.sendTemplatedEmail('campaign-email', templateVariables, emailOptions); + console.log('DEBUG: About to call sendTemplatedEmail from sendCampaignEmail...'); + const result = await this.sendTemplatedEmail('campaign-email', templateVariables, emailOptions); + console.log('DEBUG: sendCampaignEmail received result:', result); + return result; } async sendTestEmail(subject, message, testRecipient = null) { @@ -364,29 +386,6 @@ class EmailService { throw error; } } - - async sendEmail(emailOptions) { - try { - if (!this.transporter) { - throw new Error('Email transporter not initialized'); - } - - const mailOptions = { - from: `${process.env.SMTP_FROM_NAME || 'BNKops Influence'} <${process.env.SMTP_FROM_EMAIL || 'noreply@example.com'}>`, - to: emailOptions.to, - subject: emailOptions.subject, - text: emailOptions.text, - html: emailOptions.html - }; - - const info = await this.transporter.sendMail(mailOptions); - console.log(`Email sent: ${info.messageId}`); - return info; - } catch (error) { - console.error('Failed to send email:', error); - throw error; - } - } } module.exports = new EmailService(); \ No newline at end of file