How do I send PHP mail via SMTP or with my cPanel email?

Short answer: create a mailbox in cPanel, then send through your own mail server (mail.yourdomain.com) on port 465 or 587 with your full email address and password. Use PHPMailer in PHP code, or an SMTP plugin in WordPress. This is more reliable than PHP's built-in mail() and is much less likely to land in spam.

Why use SMTP instead of mail()

  • Mail sent with mail() is often not signed or authenticated, so Gmail, Yahoo and Outlook may put it in spam or reject it.
  • SMTP logs in with a real mailbox, so the message is authenticated and encrypted.
  • You get clear error messages when something fails.

Step 1: Create a mailbox

  1. Log in to cPanel and open Email Accounts.
  2. Click Create. Choose an address such as no-reply@yourdomain.com and a strong password.
  3. Click Create to save.
The cPanel Create an Email Account formcPanel, Email Accounts, Create: choose the name and a strong password.

Step 2: Note the settings

In Email Accounts, click Connect Devices next to the mailbox. cPanel shows the exact values for your account. Normally they are:

  • SMTP host: mail.yourdomain.com
  • Port: 465 (SSL) or 587 (TLS)
  • Username: your full email address
  • Password: the mailbox password
The cPanel Connect Devices page with the manual mail settings, showing the incoming IMAP and outgoing SMTP server and portsThe Connect Devices page shows the server names and ports for this mailbox.

Step 3A: WordPress

  1. In WordPress go to Plugins, then Add New, and install an SMTP plugin.
  2. Open the plugin settings and choose Other SMTP.
  3. Enter the host, port, encryption, username and password from Step 2.
  4. Set the From address to the same mailbox.
  5. Send a test email to yourself.

Step 3B: PHP code with PHPMailer

Install PHPMailer with Composer:

composer require phpmailer/phpmailer

Then use it like this:

use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host       = 'mail.yourdomain.com';
$mail->SMTPAuth   = true;
$mail->Username   = 'no-reply@yourdomain.com';
$mail->Password   = getenv('SMTP_PASSWORD');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port       = 465;
$mail->setFrom('no-reply@yourdomain.com', 'Your site');
$mail->addAddress('someone@example.com');
$mail->Subject = 'Test';
$mail->Body    = 'It works.';
$mail->send();

Keep the password out of your code. Use an environment variable, or a config file outside public_html.

Using the basic mail() function

If you only need a simple script, mail() works on our hosting without any setup. Send from an address on your own domain, for example From: no-reply@yourdomain.com. Mail sent from another domain is often rejected. For anything important, use SMTP as shown above.

Common problems

Authentication failed

Check that the username is the full email address, and the password is correct. Try logging in to webmail with the same details.

Cannot connect to the server

Check the host name and port. Try 587 with TLS if 465 does not work. Some home or office networks block mail ports.

Certificate or TLS error

Use mail.yourdomain.com only if your domain has a valid SSL certificate. Do not turn off certificate checks in production.

Mail still goes to spam

Make sure SPF, DKIM and DMARC are set for your domain. See How do I verify, check DKIM, SPF, TXT record. Also avoid sending from a different domain than the mailbox.

Can I send newsletters this way?

A hosting mailbox has a sending limit per hour. For newsletters or bulk email use a proper email sending service.

Still stuck? Open a support ticket with your domain name and what you see. Please do not put passwords in the ticket.


Related tutorial

  • 15 Users Found This Useful
  • php email, smtp email, sendmail, cpanel send email php how to enable php mail function in cpanel , cpanel send email php, smtp settings cpanel, cpanel smtp, cpanel email, wordpress email, smtp for wordpress, woocommerce email, website email, email notification
Was this answer helpful?
Still stuck? Ask our AI assistant for help troubleshooting this.

Related Articles

How to sign up for AkolagTech hosting plan - Video

How to sign up for AkolagTech hosting plan?Video Tutorial on how to sign up for hosting plan

AkolagTech hosting platform login and navigation - Video

How to log-in and navigate to AkolagTech platform video tutorial 

AkolagTech Hosting Platform Tutorial (Web, Email, Cloud) info

How to log-in and navigate through the hosting platform  Access to your product and services...

HOW TO INSTALL SOFTWARE, PLUGINS, AND APPS ON YOUR AKOLAGTECH HOSTING ACCOUNT?

ABOUT THE HOSTING PLATFORM AkolagTech hosting platform for SME/SMB, developers (web developers,...

How to access AkolagTech's cPanel and single sign-on

Short answer: log in at the AkolagTech client portal and use single sign-on to reach your cPanel,...