PHPMailer gains its popularity due to its advanced features in sending emails. It supports methods to authorize users, send attachments and more.
But, choosing PHPMailer authtype depends on the specific mail server configuration. Often, using wrong auth types can result in email errors.
At Bobcares, we solve many PHPMailer errors as part of our Server Management Services.
Today, we’ll see how our Support Engineers help customers choose the right PHPMailer auth type and fix common errors with it.
What is PHPMailer?
In simple words, PHPMailer is a program that helps to send mails from web applications.
Unlike normal PHP mail function, it supports security methods like message encryption, authentication, etc. Additionally, PHPMailer can send attachments and even HTML messages. That’s why, popular applications like WordPress, Joomla, etc. include PHPMailer in their mail programs. For example, wp_mail function in WordPress relies on the PHPMailer class to send email through PHP’s mail function.
Again, one of the most effective feature of PHPMailer is that it supports several authentication mechanisms. These auth methods avoid the sending of unknown emails. Thus, it can greatly help on reducing spamming.
That’s why, our Support Engineers usually recommend shared server owners to setup mail programs that support authentication methods. Essentially, properly updated PHPMailer packages does the mail sending jobs beautifully.
Overview of PHPMailer authtypes
As we already saw, in shared servers with too many accounts, anonymity can be an advantage to email spammers. Fortunately, using the proper authtype makes emails from PHPMailer more accountable. It helps to allow only emails from valid users.
Now, let’s see the top auth types that PHPMailer supports.
The authtype options in PHPMailer are CRAM-MD5, LOGIN, PLAIN, XOAUTH2. If there is no specific option set in the PHPMailer config file, it tries each of these mechanisms. In case of wrong logins, when one auth type fails, others also will result in error.
We’ll now check what each of these auth options do.
PLAIN
In auth PLAIN mechanism, the server converts the email user name and password to base64 encoded credentials. And, anybody can easily decrypt this data. Therefore, this allows even the attacker to decrypt it and misuse the data. This is why, our Dedicated Engineers always secure the connection with SSL/TLS while having a session with the SMTP server.
LOGIN
Similarly, LOGIN is another method of authentication in servers. Here, after sending the AUTH LOGIN command, the mail server asks for username and password by sending BASE64 encoded questions to the client. Then, client sends the encrypted username and password. Again, this method becomes insecure without proper mail settings.
CRAM-MD5
To overcome the drawbacks of AUTH and PLAIN, there comes an authentication mechanism with the name CRAM-MD5. This uses a complex method to send messages between the server and client. Also, the response send by the server will be valid only for one time. Thus, it helps to avoid hacker using it again.
XOAUTH2
XOAUTH2 is mainly used in Google authentication. Mainly, this method relies on the league/oauth2-client library. So, for customers who wish to use Gmail servers in their PHPMailer config, we preset the server with necessary modules.
However, in real time environment, mail server may not support all auth types. Therefore, it is really necessary to choose the authtype in PHPMailer as per the app requirement.
In general, to know about the exact auth types supported by the mail server, our Support Engineers do a telnet to port 25 of the mail server. The output shows up as below.
220 domain.com ESMTP MailCleaner (Community Edition 2018.12) Tue, 05 Mar 2019 13:47:32 +0530
EHLO domain.com
250-domain.com Hello domain.com [172.17.252.159]
250-SIZE 104857600
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
For instance, this mail server supports PLAIN, LOGIN mechanisms.
And, our Support Engineers set PHPMailer authtype to PLAIN using the following option in configuration file.
/* Set AuthType to PLAIN. */
$mail->AuthType = 'PLAIN';
Common errors with PHPMailer authtype and fixes
However, from our experience in managing PHP applications, we often see application errors due to bad selection of PHPMailer authtype. Let’s now see how our Support Engineers diagnose and fix them.
1. Firewall restrictions
Recently, one of our customers reported problems with using Gmail server in his PHPMailer configuration. The error showed up as:
2018-12-19 08:25:29 Connection failed. Error #2: stream_socket_client():
unable to connect to smtp.gmail.co.za:587 (Connection timed out)
[/home/xxx/public_html/PHPMailer-master/src/SMTP.php line 326]
2018-12-19 08:25:29 SMTP ERROR: Failed to connect to server: Connection
timed out (110) SMTP connect() failed.
On checking the reason for the error, we first confirmed that the authtype is properly set to XOAUTH2. But, we could see that the outgoing mail port 587 was closed. Here, our Support Engineers modified the firewall to allow mail traffic on port 587 to Gmail servers. And, the mail started working.
2. AUTH not accepted from server
Similarly, there can be problems when PHPMailer uses an auth type that is not supported by the mail server. Here, we fix the problem by editing the PHPMailer configuration to use the correct authtype. On secure mail servers, we set the authentication type as PLAIN and mail starts working.
[Need help to fix PHPMailer errors? We can help you.]
Conclusion
In short, PHPMailer adds more security to emails by allowing different authtypes. Today, we saw the different auth mechanisms in PHPMailer and how our Support Engineers help customers to choose the right PHPMailer authtype.
0 Comments