External SMTP for email sending
Sending emails through SMTP (Simple Mail Transfer Protocol) is a common way to send emails over the Internet. It works by exchanging commands and data securely over a dedicated connection. This tutorial will guide you through setting up email sending via SMTP using PaaS PHP hosting.
PHPMailer for Email Sending
PHPMailer is a classic, full-featured email-sending class for PHP, compatible with PHP version 5.0 and higher. Let’s examine how to integrate it into the platform.
Step 1. Log in to the platform dashboard.
Step 2. Create your PHP environment.
- Press the “Create environment” button.
- Navigate to the PHP tab in the dialog box that appears.
Step 3. Configure the environment.
- Select the Apache application server.
- Specify the cloudlet limits for the server.
- Enable the Public IP feature for the Apache node.
- Name your new environment (for example, “phpmailer”) and click “Create.”
Step 4. Download the PHPMailer script from GitHub.
Step 5. Upload the script to the platform.
- Navigate to the Deployment Manager.
- Browse the package and deploy it to the environment by clicking the Deploy button.
Step 6. Configure the application server.
- Click the“Config” button next to your application server.
- Within the ROOT directory, locate the index.php file. If it’s not already present, create a new file named index.php
CODE:
<?php
// Include PHPMailer classes
require 'src/PHPMailer.php';
require 'src/SMTP.php';
require 'src/Exception.php';
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Create a new PHPMailer instance
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
// Server settings (you may need to modify these)
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.example.com'; // Specify main and backup SMTP servers
// $mail->SMTPAuth = true; // Enable SMTP authentication
// $mail->Username = 'your_smtp_username'; // SMTP username
// $mail->Password = 'your_smtp_password'; // SMTP password
// $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
// $mail->Port = 587; // TCP port to connect to
// Sender and recipient settings
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name'); // Add a recipient
// $mail->addReplyTo('[email protected]', 'Reply To');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject of the email';
$mail->Body = 'HTML message body';
$mail->AltBody = 'Plain text message body';
// Send email
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Step 7. Specify all the necessary SMTP details in the index.php file to configure mailing via localhost or through a specific email account:
You can use the following configuration as an example:
- host: ssl://smtp.gmail.com OR your email server host
- port: 465/587 (SMTP port of your email provider)
- username: state the email account you would like to use
- password: state the password to the account you’ve specified in the username line
- addreply and replyto: state the same email as in the username line (the one you are going to use for mailings)
Please Save the modifications you’ve made. Then, by clicking the “Open in Browser” option adjacent to your environment, you can view the result, which should display the message “Message has been sent” upon successful execution.
In a few minutes, this email will appear in your inbox.
This guide has equipped you with the knowledge to set up SMTP for sending emails using the PHPMailer function. You’ve learned how to configure your PHP environment, deploy PHPMailer, and customize your SMTP settings.