25

I have the same php script running on localhost - my PC with XAMPP and on a hosted server. It works from my PC, but not from the hosted server.

When I send it from the hosted server, I get the following output:

SMTP -> ERROR: Password not accepted from server: 535 Incorrect authentication data  
SMTP -> ERROR: RCPT not accepted from server: 550-Please turn on SMTP Authentication in your mail client, or login to the 550-IMAP/POP3 server before sending your message. dev.camppage.com 550-(patchvalues.com) [205.234.141.238]:50958 is not permitted to relay through 550 this server without authentication.  
SMTP Error: The following recipients failed: [email protected] FAILED

I suspect there is a configuration setting that needs to be changed on the server, but I don't know which one. Any advice would be greatly appreciated!

Here is the code:

function send_gmail ($recipients, $subject, $message, $attachment_filenames = array()) 
{
  global $email_address, $email_password, $email_name;
  require_once ($_SERVER['DOCUMENT_ROOT']. '/php/PHPMailer/class.phpmailer.php');   

  $body  = $message;
  $body  = str_replace("\\", '', $body);
  $mail = new PHPMailer();
  $mail->CharSet = "UTF-8";
  $mail->IsSMTP();
  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
  $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing) 0 - none; 1 - errors & messages; 2 - messages only
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
  $mail->Port       = 465;                   // set the SMTP port
  $mail->Username   = $email_address;  // GMAIL username
  $mail->Password   = $email_password; // GMAIL password
  $mail->SetFrom($email_address);
  $mail->FromName   = $email_name;
  $mail->AddReplyTo($email_address,$email_name);
  $mail->Subject    = $subject;
  $mail->MsgHTML($body);
  $mail->IsHTML(true); // send as HTML

  if (isset ($recipients[0]))
  {
    foreach ($recipients AS $to)
    {
        $to_pieces = explode (",", $to, 2);
        $to_email = trim ($to_pieces[0]);
        if (isset ($to_pieces[1]))
            $to_name = trim ($to_pieces[1]);
        else
            $to_name = " ";
        $mail->AddAddress($to_email, $to_name);
    }
    $mail->IsHTML(true); // send as HTML

    if ($mail->Send()){
        return TRUE;
    } else {
        return FALSE;
    }
} 
else 
{
    return FALSE;
}
}

TIA

1

3 Answers 3

64

The solution was to enable outgoing SMTP from the server settings.

On servers running cPanel's WHM, this is located under WHM's "Tweak Settings" section.

The option is to enable/disable - choose disable.

Caveat: Making this change will redirect outgoing SMTP connections allow accounts to make direct connections which may increase your odds of getting your server blacklisted.

7
  • 12
    You are life saver. I'm using CentOs 6 Login to CPanel > Tweak Settings > All> "Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)" <== disable it. I describe it more detail for any one that got same problem Commented Mar 20, 2013 at 8:50
  • 3
    Dude, you are a life saver, I was right in front of the firing squad of frustration when you came from nowhere with your hero suit to save me. Thanks
    – Temitayo
    Commented Dec 10, 2015 at 14:24
  • Yup. Saved my skin too. Thanks! Commented Feb 8, 2018 at 15:17
  • 1
    GOLD METAL. I spend more than one year and was just so simple. Thanks also to @Telvin Nguyen that made it even simpler
    – theok
    Commented Jan 5, 2019 at 23:01
  • Is anyone able to provide more information about why changing this setting increases the chance of getting your IP on blacklists? How can we use PhpMailer on a WHM server, with a Gmail account without getting our IP black listed if this setting is necessary to change? I have been battling this for months and cant believe there is not a logical solution. Thanks,
    – wuno
    Commented May 24, 2020 at 8:52
9

this is related to WHM/cPanel , you can do the same thing as in the previous answer or from shell by typing this

/scripts/smtpmailgidonly off

you should get

"SMTP Mail protection has been disabled.  All users may make outbound smtp connections."

more reading here https://forums.cpanel.net/threads/cant-enable-smtp-restrictions.360971/

0
3

I am writing this answer because commenting supports only text and does not provide an option to upload an image. The answer given by @John Dorner is the correct one.

I am posting this image for quick reference. This is only for that any future user who comes here can quickly fix this issue.

Note: This kind of issue usally happens when you're setting up a server and there may be a possibility that nameservers are managed by the domain service provider and server is pointed with just A record entry with some IP address. In such a case there may be a possibility that the domain owner is using GSuit and added few MX records to name records of that domain. In such a situation, sending email through an email account created through WHM may not work because of missing MX record entry on the server and that MX record entry exist at the domain service provider. In this situation it is better you use third-party email service provides such as MailGun, MailTrap or MailChimp.

enter image description here

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