4

I'm trying to use PHPmailer to send mails. My webhost has said if the mail is relayed through their datacenter, no credentials are required. This is my code:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

$mail->SMTPSecure = 'ssl';
$mail->SMTPDebug    = 2;
$mail->isSMTP();

$mail->Host         = 'smtpgateway.webhost.com';
$mail->SMTPAuth     = false;
$mail->SMTPSecure   = false;
$mail->port         = 25;
$mail->setFrom('info@mydomain.com', 'Test');
$mail->Subject      = $email_subject;
$mail->Body         = $email_body;
$mail->addAddress($email, $name);
$mail->isHTML(true);
if($mail->send())
{
    echo "Success";
}

But I get this error when trying to send email:

2018-08-21 10:07:03 CLIENT -> SERVER: MAIL FROM: info@mydomain.com

2018-08-21 10:07:03 SERVER -> CLIENT: 250 OK

2018-08-21 10:07:03 CLIENT -> SERVER: RCPT TO:test@example.com

2018-08-21 10:07:03 SERVER -> CLIENT: 550-Please turn on SMTP Authentication in your mail client. (mydomain.com)550-[10.100.15.115]:41032 is not permitted to relay through this server without550 authentication.

2018-08-21 10:07:03 SMTP ERROR: RCPT TO command failed: 550-Please turn on SMTP Authentication in your mail client (mydomain.com)550-[10.100.15.115]:41032 is not permitted to relay through this server without550 authentication.

Community
  • 1
  • 1
Inception
  • 455
  • 1
  • 9
  • 32

3 Answers3

1

If this was purely about authentication, I would expect that first MAIL FROM command to fail. The "relaying" message should be read like this:

(mydomain.com) is not permitted to relay through this server without authentication.

This suggests that this server doesn't host email for either the FROM or TO domains, i.e. it's relaying, and relaying without authentication makes it an open relay, which is a bad thing (unless it's inaccessible from outside). I would guess that you may need to use a different from domain for it to work without authentication, and look earlier in the SMTP transcript (at the response to EHLO), which will show whether the server actually support authentication or not.

SMTP is nearly always preferable to using the PHP mail() function; mail() is slower and less safe. All a sendmail binary does is open a synchronous SMTP connection to localhost anyway, so you're skipping an unnecessary process by doing it directly. Neither route makes any guarantees about speed of delivery - SMTP is a store-and-forward protocol, and operations can be extremely slow, which is why you want to hand off the job to a local mail server.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Hi, Thank you for your response. Here is my code and the response I received: pastebin.com/AYhwygg4 It seems authentication is supported? I tried changing the from address to a random gmail one (without authentication) and it gave me the same response. My webhost provided me with an SMTP gateway (smtpgateway.myhost.com) and said it's not a mailserver. My preference is to use SMTP for the safety reasons I've read about it. – Inception Aug 22 '18 at 05:47
  • Sounds like your host is clueless. An SMTP gateway *is* a mail server. If they require auth, get support from them about it. Not really anything we can do from here. – Synchro Aug 22 '18 at 06:02
  • 1
    LOL thanks for clearing it up. I was extremely confused when they said it. So from the debug log, it is clear they require auth, I guess I'll ask them about it! Thanks a lot! – Inception Aug 22 '18 at 06:06
  • My webhost got back to me. They are saying they faced no issues sending mails from their server. They have sent me a email to my outlook domain as proof. I'm still stuck with this code: https://pastebin.com/raw/9nKzTWkB and the same 'Turn on authentication in your client' error :| – Inception Aug 28 '18 at 06:46
  • Try using the From address they used. – Synchro Aug 28 '18 at 06:48
  • They used a dummy mail from my domain (that does not actually exist). (info@mydomain.com) – Inception Aug 28 '18 at 06:49
  • My domain does not have SSL. Does this somehow cause the problem? When I comment out the SMTPOptions and SMTPSecure options, I get 'Could not connect to host' error -> https://pastebin.com/raw/6CUPpGbR – Inception Aug 28 '18 at 06:55
  • Just to clarify, they did use the mail I tried used (info@mydomain.com) – Inception Aug 28 '18 at 07:03
0

Try without any of the SMTP details as you potentially don't need to use SMTP from what your host has said.

$mail->setFrom('info@mydomain.com', 'Test');
$mail->Subject      = $email_subject;
$mail->Body         = $email_body;
$mail->addAddress($email, $name);
$mail->isHTML(true);
if($mail->send())
{
    echo "Success";
}

Worth a try, let me know if it doesn't work, but I'm guessing you don't need SMTP here.

Check this out, similar issue: PHPmailer without using SMTP

Adam
  • 1,294
  • 11
  • 24
0

Please use this code .make sure you have given permission in Mail account from you want to send mail.

My Account->Sign-in & security->Allow less secure apps: OFF

        $mail->CharSet = 'UTF-8';
    $mail->SMTPDebug = false;                                 
    $mail->isSMTP();                                      
    $mail->Host = 'smtp.live.com';                         
    $mail->SMTPAuth = true;                               
    $mail->Username = 'mail@gmail.com';                 
    $mail->Password = 'Password';                           
    $mail->SMTPSecure = 'tls';                            
    $mail->Port = 587;                                    
    $mail->setFrom($details['from'], $details['from']);
    if(is_array($details['to'])){
        foreach ($details['to'] as $key => $value) {
            $mail->addAddress($value['email'], $value['name']);
        }
    }else{
        $mail->addAddress($details['to'], isset($details['name'])?:$details['to']);
    }

    $mail->isHTML(true);
    $mail->Subject =$details['subject'];
    $mail->Body    =$details['body'];
    $mail->send();
Piyush Kumar
  • 48
  • 1
  • 4