title: How to use Mailgun's recipient-variables with Django SMTP mail backend?
body: <p>How can I properly send <a href="https://documentation.mailgun.com/en/latest/user_manual.html?#batch-sending" rel="nofollow noreferrer">batch/bulk/mass emails</a> using MailGun in Django using SMTP protocol?</p>
  <h3>What I've tried so far?</h3>
  <ul>
  <li>I am using <a href="https://docs.djangoproject.com/en/3.2/topics/email/#django.core.mail.backends.smtp.EmailBackend" rel="nofollow noreferrer"><strong><code>django.core.mail.backends.smtp.EmailBackend</code></strong></a> as my <strong><code>EMAIL_BACKEND</code></strong></li>
  </ul>
  <p>and this is the code snippet that I have tried to send the emails.</p>
  <pre class="lang-py prettyprint-override"><code>from django.core.mail import EmailMultiAlternatives
  import json
  
  to_emails = [
      &quot;mail_1@example.com&quot;,
      &quot;mail_2@example.com&quot;,
      &quot;mail_3@example.com&quot;,
      &quot;mail_4@example.com&quot;,
      &quot;jerinpetergeorge@gmail.com&quot;,
  ]
  mail = EmailMultiAlternatives(
      subject=&quot;Hey - %recipient.name%&quot;,
      body=&quot;Hey %recipient.name%,\n\nThis is just a batch email test!!!&quot;,
      from_email=&quot;JPG &lt;me@somehost.com&gt;&quot;,
      to=to_emails,
  )
  recipient_variables = {
      address: {&quot;name&quot;: address} for address in to_emails
  }
  mail.extra_headers[&quot;X-Mailgun-Recipient-Variables&quot;] = json.dumps(recipient_variables)
  response = mail.send()
  print(response)
  </code></pre>
  <p>and I've got the mail as below,
  <a href="https://i.stack.imgur.com/3iowm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3iowm.png" alt="Received Email" /></a></p>
  <p>As we can see, the <em><strong><code>to</code></strong></em> attribute is filled with all email addresses, <em><strong>which is not what I am expecting</strong></em>.</p>
  <p>So, how can I tell the Mailgun/Django to parse my variables properly in order to make the emails looks more personal?</p>
  <hr />
  <p><strong>Notes</strong></p>
  <ul>
  <li>I prefer to use SMTP protocol</li>
  <li>I've tried the REST APIs of Mailgun and it was a success (but, I prefer SMTP)</li>
  <li>I found <a href="https://pypi.org/project/django-anymail/" rel="nofollow noreferrer">django-anymail</a> and seems it has the feature. But, It also uses the APIs (correct me if I am wrong)</li>
  </ul>
  <hr />
  <h2>Update-1</h2>
  <ul>
  <li>Updated the <strong><code>to</code></strong> argument to <strong><code>to=&quot;%recipient%&quot;</code></strong> But, got
  <ul>
  <li>
  <blockquote>
  <p>TypeError: &quot;to&quot; argument must be a list or tuple</p>
  </blockquote>
  </li>
  </ul>
  </li>
  <li>Updated the <strong><code>to</code></strong> argument to <strong><code>to=[&quot;%recipient%&quot;]</code></strong> But, got
  <ul>
  <li>
  <blockquote>
  <p>smtplib.SMTPRecipientsRefused: {'=?utf-8?q?=25recipient=25?=': (501, b'Invalid command or cannot parse to address')}</p>
  </blockquote>
  </li>
  </ul>
  </li>
  </ul>