Sending an e-mail from a PHP script

For sending an e-mail message from a PHP script we recommend to use the class Mail 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 you can read documentation related to 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 reports of problems or other inducements 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.

  • On the phone from 8:30 - 17:30
  • Sometimes even on weekends
  • Simple human approach
  • Complex problems solving
contact form

Give a chance to our online help-center. You can find there more than:

  • 6 categories
  • 61 manuals
  • 23 e-mail settings
  • 9 programming tips
go to counseling

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

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

Thanks for message

If necessary, we will contact you.

Send of message failed

Please contact us at helpdesk@platon.net.

NEWSLETTER

Subscribe to receive our news and current offers

Thanks for your subscribe

Subscribtion has failed