Sending emails via Mailgun in Appwrite functions: Any solutions?

I’m trying to figure out how to send emails using Mailgun within an Appwrite function. My current setup isn’t working and I’m getting an ‘Unauthorized’ error. Here’s what I’ve got so far:

async function deliverMessage(recipient, logger) {
  logger('Starting email delivery process');
  logger('Recipient address:', recipient);
  
  // Mailgun setup code would go here
  
  // Attempt to send email
  try {
    // Email sending logic
  } catch (error) {
    logger('Error:', error.message);
  }
}

When I run this, I get:

Native logs detected. Use logger.info() or logger.error() for better logging.
Error: Unauthorized

I’m pretty sure my Mailgun setup is correct, but something’s not clicking. Any ideas on what I might be missing or how to troubleshoot this? Thanks for any help!

hey mate, i had similar issues. make sure ur mailgun api key is correct and ur using the right domain. also, check if ur function has proper network access. sometimes appwrite’s security settings can block outgoing requests. good luck!

I encountered a similar issue when integrating Mailgun with Appwrite functions. One crucial step is to properly set up environment variables for your Mailgun credentials. In your Appwrite console, navigate to the Functions section and add environment variables for your Mailgun API key and domain. Then, in your function code, access these variables using process.env. This approach keeps your credentials secure and separate from your code. Additionally, ensure you’re using the correct Mailgun SDK version compatible with your Node.js environment in Appwrite. If the issue persists, try logging the specific error details to pinpoint the exact cause of the ‘Unauthorized’ error.

I’ve been working with Mailgun in Appwrite functions recently, and I can share some insights. First, double-check your Mailgun API key and domain in the Appwrite console’s environment variables. Sometimes, it’s easy to miss a character or two.

Also, make sure you’re using the latest Mailgun-js package. I had issues with an older version that weren’t immediately apparent. Here’s a snippet that worked for me:

const formData = require('form-data');
const Mailgun = require('mailgun.js');
const mailgun = new Mailgun(formData);

const mg = mailgun.client({username: 'api', key: process.env.MAILGUN_API_KEY});

mg.messages.create(process.env.MAILGUN_DOMAIN, {
    from: 'Your Name <[email protected]>',
    to: recipient,
    subject: 'Hello',
    text: 'Testing some Mailgun awesomeness!'
})
.then(msg => console.log(msg))
.catch(err => console.log(err));

If you’re still getting ‘Unauthorized’, try logging the full error object. It might give more detailed information about what’s going wrong. Hope this helps!