Search code examples
phpsmtpphpmailer

PHPMailer works with request from localhost but not from outside my machine


There are many posts trying to figure it out that PHPmailer works in localhost but not in server, but my problem is way weirder!

I have a script working fine on my local machine (XAMP on osx). I'm using SMTP on my server and it's all working great!

    <?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include 'PHPMailer/src/PHPMailer.php';
include 'PHPMailer/src/Exception.php';
include 'PHPMailer/src/SMTP.php';

    $serverEmail = 'alarm_api@mydomain.com';
    $email1 =  $_GET['el1'];

    $mail = new PHPMailer(true);

    // SMTP config
    $mail->isSMTP();
    $mail->Host = 'lx14.hoststar.hosting';
    $mail->SMTPAuth = true;
    $mail->Username = $serverEmail; 
    $mail->Password = '***********'; 
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port = 465;
    /* Disable some SSL checks. */
    $mail->SMTPOptions = array(
        'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
        )
    );

    // Headers
    $mail->setFrom($serverEmail, 'Alarm system');
    $mail->addReplyTo($serverEmail, 'Alarm system');
    $mail->addAddress($email1, $email1); 
    $mail->Subject = 'Something is wrong';
    $mail->isHTML(true);

    $mailContent = 'Some information..';

    $mail->Body = $mailContent;

    if($mail->send()){
        print_r("Sending: OK".$br);
    }else{
        print_r("Sending: ERROR".$br);
    }
?>

So my url is localhost/api.php?el1=email@server.com and that is working perfectly.

The problem!

If i call the same php file from other sources, like my phone or another computer on local network, I'll do something like 10.0.0.8/api.php?el1=email@server.com, which is basically the same thing.

But it fails! All sort of erros pop up, like:

Fatal error: Uncaught exception 'PHPMailer\PHPMailer\Exception' with message 'The following From address failed: alarm_api@mydomain.com.br : MAIL FROM command failed,Access denied - Invalid HELO name (See RFC2821 4.1.3) ,550,SMTP server error: MAIL FROM command failed Detail: Access denied - Invalid HELO name (See RFC2821 4.1.3) SMTP code: 550' in /Users/myself/eclipse-php/PHPMailer/src/PHPMailer.php:1869 Stack trace: #0 /Users/myself/eclipse-php/PHPMailer/src/PHPMailer.php(1601): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Mon, 31 A...',

and so on.

What could be causing the script to fail when being calling from a different machine? Is the same apache, same php, same PHPmailer, same file, same access to the SMTP server, same credentials...

That was quite long. Thank you all for your time!


Solution

  • It's the HELO name that's the problem. If you don't set it explicitly, PHPMailer tries to make one up in this code. On a private network such as 10.0.0.0/8, it may end up with the IP address as the HELO name, which isn't allowed by itself, however, you can wrap it in square brackets as per RFC5321 § 4.1.3. PHPMailer doesn't do this for you, but you can set a HELO name explicitly via the Helo property:

    $mail->Helo = 'localhost.localdomain';
    

    or

    $mail->Helo = '[10.0.0.8]';
    

    Give those a try.