Customer Support

  1. Support
  2. Email
  3. Sending email via PHP: Why you should avoid the mail() function and how to send properly via SMTP
  1. Home
  2. Knowledge Base
  3. Email Clients
  4. Sending email via PHP: Why you should avoid the mail() function and how to send properly via SMTP

Sending email via PHP: Why you should avoid the mail() function and how to send properly via SMTP

The mail() function is PHP's default method for sending messages. While simple to implement, it has serious technical drawbacks:

  • No Authentication: It dispatches messages directly from the local web server using an internal system user (e.g., systemuser@server.papaki.gr), which does not correspond to a real, authorized email account.
  • Fails Security Verification (SPF, DKIM, DMARC): Major email providers (Gmail, Yahoo, Outlook) mandate strict sender authentication. Emails generated via the basic mail() function fail these checks, leading receiving servers to reject them or route them straight to Spam.
  • Poor Header & HTML Handling: It lacks native support for modern HTML standards, proper character encoding (UTF-8 for multilingual text), or secure attachment handling.

To ensure your website's emails land reliably in your recipients' inboxes, all outgoing messages should be sent through an authenticated SMTP mail server, using a valid email account on your domain (e.g., info@yourdomain.gr or noreply@yourdomain.gr).

Required SMTP Settings:

  • Outgoing Mail Server (SMTP Server):
    • smtp-gr.securemail.pro, if you are using an email package from the new Papaki, or
    • your server's hostname, e.g. linux262.papaki.gr, or
    • mail.yourdomain.gr, provided that the corresponding DNS record has been created in your domain's active DNS zone
  • Port: 465 with SSL/TLS encryption (or 587 with STARTTLS)
  • Authentication (SMTP Authentication): Enabled (Yes / Required)
  • Username: Your complete email address (e.g., info@yourdomain.gr)
  • Password: The password for that specific email account

How to Configure SMTP on Your Website

A. If Your Website Uses a CMS (WordPress, Joomla, OpenCart, PrestaShop)

If you run an established content management system or e-commerce platform, you do not need to write any PHP code:

  • WordPress: Install a free SMTP plugin, such as WP Mail SMTP or Easy WP SMTP. In the plugin settings, select "Other SMTP" and enter your email connection details.
  • Joomla: Go to System > Global Configuration > Server tab > Mail Settings. Set the Mailer to SMTP, enable SMTP Authentication, and enter your account credentials.
  • OpenCart: In your admin dashboard, navigate to Settings > Mail tab. Switch the Mail Protocol from Mail to SMTP and fill in your server and port credentials.
  • PrestaShop: Go to Advanced Parameters > E-mail, select Set my own SMTP parameters, and enter your credentials.
  • Drupal: Install and configure the SMTP Authentication Support module.

B. If You Have a Custom PHP Application: Using PHPMailer

For custom-built websites and web applications, the standard industry practice is using the PHPMailer library, which automatically manages message headers, UTF-8 encoding, attachments, and secure SMTP authentication.

Sample Implementation:

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

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
// Server Settings
$mail->isSMTP();
$mail->Host = 'smtp-gr.securemail.pro'; // Your SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'info@yourdomain.gr'; // Your email address
$mail->Password = 'YourEmailPasswordHere'; // Your email password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL/TLS
$mail->Port = 465; // Port 465 (or 587 with STARTTLS)
$mail->CharSet = 'UTF-8'; // Character set for multilingual support

// Sender & Recipients
$mail->setFrom('info@yourdomain.gr', 'Your Business Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Message Content
$mail->isHTML(true);
$mail->Subject = 'Email Subject Here';
$mail->Body    = '<b>Hello!</b> This is a test email sent via authenticated SMTP.';
$mail->AltBody = 'This is the plain text version for email clients that do not support HTML.';

$mail->send();
echo 'Message has been sent successfully!';

} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}


Temporary Workaround (Legacy Fallback): Using the -f Flag with mail()

If you must temporarily keep using PHP's native mail() function before updating your codebase to SMTP, you must at least specify the fifth parameter (-f) to set a valid envelope sender address from your domain:

mail('recipient@example.com', 'Email Subject', 'Message body goes here', null, '-finfo@yourdomain.gr');

  • Note: The -f parameter must be immediately adjacent to your sender email address (without spaces).
  • Warning: This is an obsolete workaround and does not guarantee successful inbox delivery to strict providers like Gmail, Yahoo, and Outlook.

You haven't found what you are looking for?

Contact our experts, they will be happy to help!

Contact us