I’m having trouble with PHPMailer 6.6.0 and MailGun when sending lots of emails. Everything works fine locally, but when I use SMTP to send through MailGun, I get a 451 error after about 100 emails. The error says there’s a problem with the MIME header data.
I’ve tried setting the charset to UTF-8 for all headers and in PHPMailer, but it’s still not working right. MailGun says the error means the message MIME is messed up, maybe because of weird characters or wrong formatting.
Here’s what I’ve done so far:
Using PHP 7.4.28 and MySQL
Set PHPMailer’s CharSet to ‘utf-8’
Set MySQL charset to ‘utf8mb4’
Used utf8_encode() on all header elements
Tried both ‘base64’ and ‘quoted-printable’ encoding
Has anyone run into this before? Any ideas on how to fix it? I’m stumped and could really use some help figuring out what’s causing the parsing error.
hey, have u tried checking ur email templates? sometimes hidden characters or weird formatting can mess up MIME headers. also, make sure ur not hitting mailgun’s rate limits. they might throttle u if ur sending too fast. maybe try adding a delay between sends or use their batch api instead of smtp?
I’ve encountered similar issues when dealing with bulk email sending through SMTP. One thing that helped in my case was implementing rate limiting. MailGun, like many email services, has sending limits to prevent abuse. Try spacing out your emails, perhaps sending in batches of 50 with a short pause between each batch.
Another potential culprit could be the email content itself. If you’re dynamically generating content, ensure all special characters are properly escaped. I’ve found using a library like HTML Purifier can help sanitize content and prevent MIME-related issues.
Lastly, double-check your MailGun configuration. Sometimes, outdated API keys or incorrect SMTP settings can lead to unexpected errors. If the problem persists, MailGun’s support team is usually quite helpful in diagnosing these types of issues.
I’ve dealt with similar MIME header issues when sending bulk emails through various SMTP providers. One thing that often gets overlooked is the line length in headers. SMTP has a limit of 998 characters per line, and exceeding this can cause parsing errors.
Try wrapping your header lines using the wordwrap() function in PHP. Something like:
$header = wordwrap($header, 70, "\r\n ");
This ensures no line exceeds the SMTP limit.
Also, check if you’re using any non-ASCII characters in your subject lines or sender names. These can sometimes cause issues if not properly encoded. You might want to use the mb_encode_mimeheader() function for these fields.
If none of that works, try enabling verbose debugging in PHPMailer and analyze the full SMTP conversation. This can often reveal where exactly the parsing is failing.
Lastly, consider upgrading to the latest version of PHPMailer. There have been several MIME-related fixes in recent versions that might address your issue.