Using PHPMailer to Send Mail through PHP Christopher MaioranaUpdated on September 11, 2026 4 Minute Read You want to send mail through PHP, but PHP’s built-in mail() function doesn’t support authenticated SMTP (Simple Mail Transfer Protocol), and cPanel mail servers require authentication for outgoing mail. PHPMailer solves that problem: it is a popular, actively maintained, open-source library that adds authenticated SMTP, attachments, and HTML formatting to outgoing mail from your PHP code. This guide walks through a working example you can adapt for local development or a live InMotion Hosting account. The sample script targets PHPMailer’s current namespaced 6.x and 7.x classes (PHPMailer\PHPMailer\PHPMailer), not the older class.phpmailer.php file. Note: Download the current release of PHPMailer from its GitHub repository, or install it with Composer as shown next. Learn more about InMotion Hosting’s PHP hosting plans. Install the PHPMailer Dependencies Composer is the recommended way to add PHPMailer to your project. It downloads the library, resolves its dependencies, and generates the autoloader your script needs, all with one command. Run this from your project’s root directory: composer require phpmailer/phpmailer Composer creates a vendor/ directory and a vendor/autoload.php file. Require that file at the top of your script and every class PHPMailer needs becomes available automatically. If you cannot use Composer, download the latest release from PHPMailer’s GitHub repository instead, and require src/Exception.php, src/PHPMailer.php, and src/SMTP.php directly from the extracted archive. Add the Code to Send Mail through PHP Before you run the script, change the example values in the table to match your own sender, recipient, and mail server details. SampleReplace with[email protected]Your sender email addressmail.example.comYour SMTP server hostnamepasswordYour sender email account’s password[email protected]Your recipient’s email addressName of senderThe display name for your sender addressName of recipientThe display name for your recipient address Not sure what to put in the host, port, or username fields? Those are the same settings you would enter in an email client. See how to find your email settings in cPanel or Webmail to look them up. On an InMotion Hosting cPanel account, the username is always the mailbox’s full email address, and the recommended outgoing setting is SMTP port 465 with SSL/TLS. To use that port instead of the sample’s port 587, set Port to 465 and SMTPSecure to PHPMailer::ENCRYPTION_SMTPS. Local development: Leave $developmentMode set to true. The script then prints the full SMTP conversation and relaxes certificate checks, which is normal for a local server with a self-signed certificate. Live server: Set $developmentMode to false before you deploy. The script then hides the SMTP transcript and enforces normal certificate validation, which is what you want once mail goes out to real recipients. <?php require "vendor/autoload.php"; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; $developmentMode = true; $mailer = new PHPMailer(true); try { $mailer->SMTPDebug = $developmentMode ? SMTP::DEBUG_SERVER : SMTP::DEBUG_OFF; $mailer->isSMTP(); if ($developmentMode) { $mailer->SMTPOptions = [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ], ]; } $mailer->Host = 'mail.example.com'; $mailer->SMTPAuth = true; $mailer->Username = '[email protected]'; $mailer->Password = 'password'; $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mailer->Port = 587; $mailer->setFrom('[email protected]', 'Name of sender'); $mailer->addAddress('[email protected]', 'Name of recipient'); $mailer->isHTML(true); $mailer->Subject = 'PHPMailer Test'; $mailer->Body = 'This is a <b>SAMPLE</b> email sent through <b>PHPMailer</b>'; $mailer->send(); echo "MAIL HAS BEEN SENT SUCCESSFULLY"; } catch (Exception $e) { echo "EMAIL SENDING FAILED. INFO: " . $mailer->ErrorInfo; } A note about the closing PHP tag PHP does not require a closing ?> tag at the end of a file, and the script above leaves it out on purpose. The PHP manual recommends omitting it, because accidental whitespace or a new line after the tag sends output early, which is a classic cause of the “headers already sent” error. Two situations call for different handling: Pure PHP file: Omit the closing tag completely, exactly as the sample above does. <?php require "vendor/autoload.php"; // PHP code continues to the end of the file PHP mixed with HTML or other markup: Add ?> right before the HTML begins, then omit the closing tag again at the end of the file. <?php require "vendor/autoload.php"; // PHP code ?> <html> <body> <p>Message sent.</p> </body> </html> If the Message Does Not Send If the script prints “EMAIL SENDING FAILED. INFO: …” instead of the success message, PHPMailer threw an exception because something in the send failed. Set $developmentMode to true temporarily so PHPMailer prints the full SMTP transcript instead of the generic error message. Most failures trace back to an incorrect host, port, username, or password, so check those values against your mailbox’s settings first. If the connection itself fails or times out, work through why won’t my PHP app send mail? next. Save the script, upload it to your server, and load it in a browser. A successful run prints “MAIL HAS BEEN SENT SUCCESSFULLY” and hands the message to your mail server for delivery. If you only need to send a simple message without SMTP authentication, compare this approach with PHP’s built-in mail() function. Summarize and Research with AIShare on Social Media CM Christopher Maiorana Content Writer II Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog. More Articles by Christopher Related Articles Using PHPMailer to Send Mail through PHP Transferring Emails from Your Old Host to InMotion Hosting How to Set up FormMail SSL Certificate Lifetimes Shrink to 47 Days by 2029 Installing SSLs and Generating CSRs in cPanel How to Upload Large Files Without FTP Timeouts eCommerce – Setup your Merchant Account Gateway How to Zip and Unzip Files on Windows 11 and Windows 10 How to Transfer and Backup Website Files with Rsync .htaccess File Reference List: Rules, Fixes, and Uses