Email via External SMTP

External SMTP for Email Sending

SMTP is the standard protocol used to send email over IP networks. In a PHP environment, messages can be delivered through PHPMailer or through a custom mail form connected to the server’s mail service.

PHPMailer for Email Sending

PHPMailer is a PHP email-sending library that can be configured to deliver messages through localhost or an external SMTP account.

Create the PHP Environment

1Create the environment

Sign in to the dashboard, click Create environment, open the PHP tab, and select an Apache application server.

2Configure resources and networking

Set the Cloudlet limits, enable a Public IP for the Apache node, enter an environment name such as phpmailer, and click Create.

Create a PHP environment for PHPMailer
Create an Apache PHP environment and enable a Public IP.

Upload and Deploy PHPMailer

3Download the PHPMailer package

Download the PHPMailer package prepared for SMTP use.

4Upload the package

Open Deployment Manager and upload the downloaded archive.

Deployment Manager
Open Deployment Manager and start uploading the application archive.
Upload the PHPMailer archive
Select the local PHPMailer archive and upload it.

5Deploy the package

After the upload finishes, deploy the package to the previously created environment.

Deploy PHPMailer
Deploy the uploaded package to the PHPMailer environment.

6Open the configuration file

Click Config for the Apache node and open webroot/{context_name}/config.php.

PHPMailer configuration file
Configure the SMTP parameters in config.php.

Send Through Localhost

Messages can be sent without authenticating through a separate email account. This configuration is simple, but such mail may be more likely to be classified as spam.

ParameterValue
hostlocalhost
authfalse
usernameSender name displayed to recipients.
addreplyReply address. This value is required.
replytoUse the same address as addreply.
PHPMailer localhost configuration
Example PHPMailer settings for sending through localhost.

Save the file, click Open in Browser, enter a recipient, subject, and message, then submit the form.

PHPMailer test form
Use the test form to send a sample message.
Test email sent through localhost
Example of a message delivered through the localhost configuration.

Send Through an Email Account

An external SMTP account can improve sender identity and deliverability, although the provider may impose sending limits. The source example uses Gmail SMTP.

ParameterExample Value
hostssl://smtp.gmail.com
port465
usernameEmail account used for sending.
passwordPassword or provider-issued application credential.
addreplyUse the sender account address.
replytoUse the sender account address.
PHPMailer Gmail SMTP configuration
Example configuration for an external Gmail SMTP account.

Save the changes, open the application in a browser, submit the test form, and verify the received message.

Test email sent through Gmail SMTP
Example message delivered through an authenticated SMTP account.

Use a Custom Email Form

1Open Apache configuration files

Click Config beside the Apache server.

Apache Config button
Open Configuration Manager for the Apache node.

2Create the form file

Navigate to webroot/ROOT, or another deployment context, and create a file such as mailtest.php.

Create the mailtest.php file
Create the custom email-form file in the application context.

The source page provides this basic form and mail-command example:

<form method="POST" action="#">
  From <input type="text" name="from"><br>
  To <input type="text" name="to"><br>
  Subject <input type="text" name="subj"><br>
  Type your message <input type="text" name="text"><br>
  <input type="submit" name="sub" value="Send">
</form>

<?php
if ($_POST['sub']) {
    system(
        "echo {$_POST['text']} | mail -s {$_POST['subj']} -r {$_POST['from']} {$_POST['to']}"
    );
}
?>
Security warning: The example above passes form input to a shell command and should not be used as-is in a public or production application. Validate all inputs and use a maintained mail library or properly configured SMTP client instead.
Custom PHP email form code
The source example placed inside mailtest.php.

Open the environment in a browser and append the new filename to the URL, for example /mailtest.php.

Custom email-sending form
Fill in the sender, recipient, subject, and message fields.

Submit the form and verify that the recipient receives the message.

Email sent through the custom form
Example message sent from the custom form.

Security and Delivery Notes

  • Use the SMTP hostname, port, encryption mode, and authentication requirements supplied by the email provider.
  • Use an application-specific password or supported token when the provider does not allow the regular account password.
  • Keep SMTP passwords outside publicly accessible files and source repositories.
  • Restrict access to test forms and remove them after verification.
  • Check the Spam folder when a test message does not appear in the inbox.
  • For production mail, configure valid sender-domain records and follow the provider’s sending limits and policies.

What’s next?