Sending an e-mail from a PHP script

To send an e-mail message from a PHP script we recommend using the Mail class from the PEAR Mail.php package.


<?php
require_once 'Mail.php';
$mail = Mail::factory('mail');
$body = 'This is message text...';
$hdrs = array(
    
'From' => 'sender@example.com',
    
'Subject' => 'Message subject'
    
);
$mail->send('receiver@example.com', $hdrs, $body);
?>

For more configuration options, see the documentation for this class.

Sending via SMTP with PHPMailer

The recommended approach is to send e-mails through the SMTP server of your mailbox. This sends the message through our mail servers in the standard way and improves deliverability, including to services such as Gmail.

The following example uses the PHPMailer library. You can install it locally with Composer:

composer require phpmailer/phpmailer

If you do not use Composer directly on the web server, install PHPMailer with Composer on your computer and then upload the vendor directory to the web server together with your PHP script.

In the example script, you only need to enter the login credentials for your own mailbox and the recipient's address.


<?php

use PHPMailer\PHPMailer\PHPMailer;
use 
PHPMailer\PHPMailer\Exception;

require 
'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    
// SMTP server
    
$mail->isSMTP();
    
$mail->Host       = 'smtps.platon.sk';
    
$mail->Port       = 587;
    
$mail->SMTPAuth   = true;
    
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    
// Mailbox login credentials
    
$mail->Username = 'info@yourdomain.com';
    
$mail->Password = 'MAILBOX_PASSWORD';

    
// Sender
    
$mail->setFrom('info@yourdomain.com', 'My website');

    
// Recipient
    
$mail->addAddress('example@gmail.com');

    
// Message content
    
$mail->CharSet = 'UTF-8';
    
$mail->isHTML(true);

    
$mail->Subject = 'Test e-mail';
    
$mail->Body = '
        <h1>Test</h1>
        <p>This is a test e-mail sent through the Platon SMTP server.</p>
    '
;

    
$mail->AltBody = 'This is a test e-mail sent through the Platon SMTP server.';

    
$mail->send();

    echo 
'E-mail was sent successfully.';

} catch (
Exception $e) {
    echo 
'E-mail could not be sent: ' . $mail->ErrorInfo;
}

helpdesk

we are ready to help

currently online

agents online 2 / 6

Platon Technologies

How can we help you?

We accept problem reports and any other feedback related to the use of our products and services.

 

helpdesk@platon.net +421 222 111 321

In case you need any help, contact us by e-mail or by phone.

  • By phone from 8:30 - 17:30
  • Sometimes even on weekends
  • Simple human approach
  • Even complex problems solved
contact form

Give our online help center a try. You will find more than:

  • 6 categories
  • 60 manuals
  • 23 e-mail settings
  • 8 programming tips
go to help center

Questions about our products or any specific request? Contact us.

We are checking whether the message is spam. Please wait...

Thanks for your message

If necessary, we will contact you.

Sending the message failed

Please contact us at helpdesk@platon.net.

NEWSLETTER

Thanks for subscribing

Subscription has failed