简体   繁体   中英

php mailer SMTP Error: Could not connect to SMTP host

I have hosted my website on Plesk Hosting and was working on submitting the contact form. Installed PHP Mailer using composer. First I tried to send email using Gmail SMPT server it worked fine Second I tried to send email using my webhosting SMTP Server it is not working for me

$mail->Host = 'webmail.abc.in';      //host
$mail->SMTPAuth = false;              
$mail->Username = '******@abc.in';    
$mail->Password = '*******';              
$mail->SMTPSecure = 'tls';
$mail->Port = 25;      

I tested the SMPT server using SMTPER . it could send email using same credentials.

i dont know where the issue is..

is there any other library other then phpmailer??

$mail->SMTPAuth = true;

As simple as that, I think.

You said you have tested the credentials on SMTPer,

I'm sure you checked the "Use authentication" checkbox.

Maybe you thought you could make it false because you are not using SSL,

But this is about user authentication, not about encrypted communication.

This is a example of of code I'm using with gmail. Tested it with webhosting SMTP and worked as well.

`           $mailMsg = ADD_MAIL_MESSAGE_HERE;
            $mailto = ADD_TO_ADDRESS_HERE;
            $mail = new PHPMailer\PHPMailer\PHPMailer();
            $mail->IsSmtp();
            $mail->SMTPDebug = 0;
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'ssl';
            $mail->Host = 'smtp.gmail.com';
            $mail->Port = 465;
            $mail->IsHTML(true);
            $mail->CharSet = 'UTF-8';
            $mail->Username = ADD_USERNAME_HERE;
            $mail->Password = ADD_PASSWORD_HERE;
            $mail->SetFrom(ADD_FROM_ADDRESS_HERE);
            //-------------------------------------------
            $mail->Subject = ADD_MAIL_SUBJECT_HERE;
            $mail->Body = $mailMsg;
            $mail->AddAddress($mailto);
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
`

Are you using a Shared Hosting with Plesk? If yes, then this can probably be a port block issue (exact reason you will get only in the maillog). Looking at your code, I can see that in case of your local SMTP testing you are using port 25 while in case of Gmail it's 465.

By default, most of the shared hosting providers block the outgoing SMTP connection on port 25. This is done in order to protect the network and infrastructure from Spamming. If this is the case, then you need to contact their support to unblock the port or use some port free mode of email sending. Means instead of connecting over SMTP, connect over HTTP API for sending emails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM