Question

Why won't PHP Mail send emails to hyphenated email addresses (e.g. digital-ocean@hotmail.com)

Posted June 1, 2015 4.8k views
UbuntuPHPEmail

It works fine with email addresses without hyphens (e.g. digitalocean@hotmail.com). Does anyone know a possible cause for this?

Please ask if you need any more information.

5 comments
  • Can you share a sample of the code attempting to send the messages?

    I have never encountered this myself when sending emails via php but some googling found a number of people reporting similar issues and the primary suggestions I found there were:

    • Escape the hyphen: digital\-ocean@example.org
    • Wrap the email in quotes: "digital-ocean@example.org"
    • use angle brackets: <digital-ocean@example.org>
  • Here is the script. As you can see I’m using PHP Mailer

    require 'PHPMailer.php';
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $where = $_POST['where'];
    $message = $_POST['message'];
    $to = "";
    
    if ($where == "one") {
      $to = "email1@example.com";
    } elseif ($where == "two") {
      $to = "email-2@example.com";
    }
    
    $mail = new PHPMailer;
    
    $mail->isHTML(true);
    $mail->From = 'form@example.com';
    $mail->FromName = 'Website Contact Form';
    $mail->addAddress($to);
    $mail->Subject = "Contact Form Submission";
    $mail->Body = "<b>Name:</b> " . $name . "<br><br><b>Email:</b> " . $email . "<br><br><b>Telephone:</b> " . $tel . "<br><br><b>Where They Are:</b> " . $where . "<br><br><b>Message:</b> " . $message; 
    
    $mail->send()
    

    Thanks.

  • Looking at PHPMailer’s source code, it appears to encode a recipient email address only if there is a name. Try passing a name to addAddress(), does that work?

    $mail->addAddress($to, 'Name here');
    
  • I tried that and it didn’t work. I think it says somewhere that the name isn’t required anyway.

  • Now I can’t send emails at all. I get this line in my mail.log

    Jun  2 16:33:54 McCarthee sm-mta[22590]: t52KXqS3022588: t52KXsS3022590: DSN: Service unavailable
    

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi @McCartheee,

This looks like some kind of an encoding issue nothing else. I’m not sure it’s actually part of the phpMailer class or just the way your strings are being saved.

Additionally, try to use the most up to date PHP version and see if the error would be again experienced.

Submit an Answer