Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Send Email from a PHP Script Using SMTP Authentication

This guide shows you how to send email from a PHP script by logging in to a real mail account. This way your emails are far less likely to land in spam.

Domains and DNS2 min read6 steps1 screenshots

What do these words mean?

PHP is a language many websites use. SMTP is the method used to send email. It is like the postman. Authentication means logging in with a username and password. The mail server checks who you are before it sends your message.

PHP has a basic mail() function. It sends mail with no login, so many receivers do not trust it. SMTP authentication is better.

Screenshot: PHP has a basic mail() function. It sends mail with no login, so many receivers do not tru

What you need

  • An email account on your domain, like info@yourdomain.com. You can create one in your hosting control panel.
  • The mail server name, port and security type. Your welcome email or the panel's email settings page lists them. Ask support if you cannot find them.
  • PHPMailer. This is a free PHP library that makes SMTP easy.

Steps

  1. Install PHPMailer. If you have Composer, run this in your site folder.
composer require phpmailer/phpmailer

This downloads PHPMailer into a folder named vendor. Composer is a tool that fetches PHP libraries.

  1. Create a file, such as send.php, and paste the code below.
  2. Replace the example values with your own.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host       = 'mail.yourdomain.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'info@yourdomain.com';
    $mail->Password   = 'your-email-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port       = 465;

    $mail->setFrom('info@yourdomain.com', 'Your Name');
    $mail->addAddress('friend@example.com');
    $mail->Subject = 'Hello';
    $mail->Body    = 'This is a test message.';
    $mail->send();
    echo 'Message sent';
} catch (Exception $e) {
    echo 'Error: ' . $mail->ErrorInfo;
}

This logs in to your mail server and sends one message. Port 465 uses SSL. For port 587, use ENCRYPTION_STARTTLS instead.

  1. Upload the file to your hosting.
  2. Open it in a browser, such as yourdomain.com/send.php.
  3. Check the inbox of the receiver.
Warning: Never leave your password in a file anyone can open. Delete the test file when you finish. Keep real passwords in a protected config file.

If it fails

  • Check the username, password, host and port.
  • Make sure the sender address is the same as the logged-in account.
  • Read the error text. It often names the problem.

Need help? Open a support ticket.

Quick recap

  • SMTP authentication sends mail after logging in.
  • It lands in inboxes better than plain mail().
  • Use PHPMailer with your mail host, port, username and password.
  • Protect your password and delete test files.