1

When using drupal_mail to send an email, I get errors when display names (not emails) contain apostrophes in the "To" field.

For example, referring to drupal_mail() documentation, email addresses like "Another User <anotheruser@example.com>" are valid. "Another User <anotheruser@example.com>" validates perfectly, but "Another O'Neil <anotheroneil@example.com>" fails.

Do I need to change the last name to ONeil instead of O'Neil, or can escape it somehow?
I have also tried ""Another O'Neil" <anotheroneil@example.com>" without success.

  • Setup: Ubuntu 11.04 LTS
  • Drupal Version: 7.x
  • Mail Service: Amazon SES
  • Additional Mail Modules: "HTML Mail" and "Mail System"
2

I assume a few things:

  • You have no exotic mail transport installed, having Drupal fallback to phps' mail()
  • You deliver the mail trough a unix system. Windows deals differently with mail.

Digging into RFC2822 you will find about escaping " and some other characters.

The solution then presents itself as

$to = "\"Another O'Neil\" <anotheroneil@example.com>";
drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from);

print $to; #=> "Another O'Neil" <anotheroneil@example.com>
  • Good assumptions. :) Will add to original question in a moment for future searchers. An elegant solution to an annoying problem. Thank you. :) – Citricguy Aug 31 '11 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.