Can send mail through Usermin or GMAIL but not from PHP

Hi. I’ve created an email id info@domain.com and I’m able to send and receive mails through this id if I use Usermin. I’ve also used following settings to add this id to GMAIL (as sender) and I’m able to send mails through GMAIL as well.

Email: info@domian.com Host: mail.domian.com Username: info.domian Password: **My password** Port 587 (tls)

When I used PHPMailer in my website with following settings. I get an error that says ‘Failed to connect to SMTP server’

$mail = new PHPMailer;

$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = “mail.domian.com”;
$mail->SMTPAuth = true;
$mail->SMTPSecure = ‘TLS’;
$mail->Port = 587;
$mail->Username = “info.domian”;
$mail->Password = ‘mypassword’;
$mail->setFrom(‘info@domian.com’, ‘Website Name’);
$mail->addReplyTo(‘info@domian.com’, ‘Website Name’);

I’ve also tried Telnet to ping the host and that works too. I don’t know what’s the problem here.

Hi,

Are you receiving an error?

Like I said, the error I get is ‘Failed to connect to SMTP server’

Hi,

Sorry I had missed your error message.

Try removing the line $mail->SMTPSecure = ‘TLS’ and see if you can connect without the TLS option.

I don’t get emails after removing that line. And I get the same error as before.

Has anyone done this before? What were the settings you used?

The tutorial over at https://github.com/PHPMailer/PHPMailer/wiki/Tutorial suggests you need an autoload.php required.

And another (using TLS) over at https://codeforgeek.com/2014/11/phpmailer-ultimate-tutorial/

I know how to use phpmailer I need settings for virtualmin email. I can send emails through a gmail id just fine.

I would like to draw attention of Virtualmin engineers. Does anyone use phpmailer? What settings do you use? I need to use SMTP.

Did you make Gmail “less secure” ???

https://github.com/matomo-org/matomo/issues/8613
https://www.saleshandy.com/smtp/gmail-smtp-settings/


And, be sure you’re using require_once(‘PHPMailerAutoload.php’); instead of require_once(‘class.phpmailer.php’);

Or, instead of “less secure”, implement OAuth2:

https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

Hi Jim, the error I’m getting is “Failed to connect to SMTP server”. It has nothing to do with google. Thanks for your effort though.

To resolve this issue, log in to your Gmail account through a web browser and enable access through less secure apps . Less secure apps can make your account more vulnerable, Google will automatically turn this setting off if it’s not being used. However, bypass this security setting with a configuration tweak within your Google Email Account .

How more secure app help to protect your account?

  • Which level of access you’re giving the client before you connect your Account.

  • Client access only a relevant part of your Account, like your email or calendar.

  • Connect your Google Account to the client without exposing your password.

  • Disconnect your Google Account from the client at any time.