Sending bulk emails with CodeIgniter and Mailgun: Recipient variables issue

Hey everyone, I’m trying to send emails to multiple recipients using CodeIgniter and Mailgun with SMTP. I’ve set up the config and I’m using recipient variables, but I’m running into a problem. All the recipients can see each other’s email addresses, which isn’t what I want.

Here’s a simplified version of my code:

$config = [
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.example.com',
  'smtp_port' => 465,
  'smtp_user' => 'myuser',
  'smtp_pass' => 'mypass',
];

$recipients = [
  '[email protected]' => ['user_id' => '1001'],
  '[email protected]' => ['user_id' => '1002']
];

$this->email->set_header('X-Custom-Recipient-Vars', json_encode($recipients));
$this->email->from('[email protected]');
$this->email->to(implode(',', array_keys($recipients)));
$this->email->subject('Test Subject');
$this->email->message('Test Message');
$this->email->send();

I’ve checked the Mailgun docs, but I can’t figure out what I’m doing wrong. Any ideas on how to fix this so each recipient only sees their own email? Thanks in advance for any help!

I’ve encountered a similar issue when working with bulk emails in CodeIgniter. The problem lies in how you’re setting the ‘to’ field. Instead of using implode() to combine all recipients, you should use the bcc() method. This way, each recipient won’t see others’ addresses.

Try modifying your code like this:

$this->email->from('[email protected]');
$this->email->bcc(array_keys($recipients));
$this->email->subject('Test Subject');
$this->email->message('Test Message');

This approach should solve your privacy concern. Also, ensure you’re using the latest version of CodeIgniter’s email library, as older versions might have limitations with large recipient lists.

Remember to check Mailgun’s sending limits and consider breaking large lists into smaller batches if necessary.

hey, i’ve dealt with this before. the issue is with using ‘to’ for all recipients. switch to ‘bcc’ instead:

$this->email->bcc(array_keys($recipients));

this’ll hide everyone’s email from each other. also, make sure ur mailgun API key is set up right in the config. good luck!

I’ve been in your shoes, and I can tell you that using Mailgun with CodeIgniter for bulk emails can be tricky. One thing that worked for me was to send individual emails in a loop instead of using a single bulk send. It’s a bit slower, but it gives you more control. Here’s a rough idea:

foreach ($recipients as $email => $vars) {
    $this->email->clear();
    $this->email->from('[email protected]');
    $this->email->to($email);
    $this->email->subject('Test Subject');
    $this->email->message('Hello %recipient.user_id%');
    $this->email->set_header('X-Mailgun-Recipient-Variables', json_encode([$email => $vars]));
    $this->email->send();
}

This approach ensures each recipient only sees their own email. It also allows you to personalize each message easily. Just remember to handle any errors that might occur during the sending process, and consider using a queue system for large batches to avoid timeouts.