1. Products
  2.   Email
  3.   PHP
  4.   PHP-Mail
 
  

Free PHP API to Send Email Messages 

Open Source and Efficient PHP Email Management Library That Enables Software Developers to Compose and Send Email Messages, Add Images and Attachments, Delete Unwanted Messages & so on.

In the vast world of web and software development, the need to send emails is a ubiquitous requirement. Whether it's for user registration, password reset notifications, or any other form of communication, a reliable and efficient PHP email library can make a significant difference. One such solution that has gained popularity among PHP developers is the Nette Mail PHP Library.

PHP-Mail is a powerful and feature-rich tool designed to simplify the process of sending emails in PHP applications. Developed by the Nette framework team, this library provides a clean and intuitive interface for constructing and sending emails effortlessly. There are several important features part of the library, such as email generation in both HTML and plain text, attaching files to emails, embedding of inline images within email content, SMTP and Sendmail support, adding custom headers to your email messages, better error handling and many more.

Nette's Mail PHP Library provides a powerful and user-friendly interface for creating and sending emails in PHP. One of the standout features of the Library is its simplicity. With just a few lines of code, developers can compose and dispatch emails effortlessly. Additionally, the library supports various email transport methods, including SMTP, Sendmail, and even the ability to queue emails for later delivery. With its intuitive API and seamless integration options, it's a valuable tool for software developers looking to streamline email functionality in their projects.

Previous Next

Getting Started with PHP-Mail 

The recommended way to install PHP-Mail SDK is via Composer, please use the following command for easy installation.

Install PHP-Mail via Composer

 composer require nette/mail

You can also download it from GitHub and manually install it with ease.

Sending HTML & Plain Text Emails via PHP

The open source PHP-Mail library makes it easy for software developers to compose and send Email messages in HTML as well as in Plain Text inside PHP applications. Generating email messages is effortless with PHP Mail library. You can set the sender, recipient, subject, and body of the email with minimal code. The library fully supports both HTML and plain text email content, allowing software developers to craft visually appealing emails while ensuring compatibility with clients that prefer plain text. The following example shows how easily software developers can send simple email using PHP.

How to Send a Simple Email Message using PHP Code?

use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;

$message = new Message();
$message->setFrom('sender@example.com')
        ->addTo('recipient@example.com')
        ->setSubject('Hello Nette Mail!')
        ->setBody('This is a test email sent using Nette Mail.');

$mailer = new SendmailMailer();
$mailer->send($message);

Add Attachments to Emails via PHP API

Sending files as attachments with an Email messages is a common requirement, and the open source Nette Mail Library makes it a breeze. The library provides a convenient method for attaching files to emails, enhancing the communication capabilities of your application. Software developers can attach files from the local filesystem or provide file contents directly. Embedding images within email content is made easy by Nette Mail. You can specify inline images using CID (Content-ID) references. The following example demonstrates how software developers can attach a file as well as an inline image to an email messages inside PHP applications.

How to Attach a File or an Inline Image to an Email Message via PHP API?

// Attach a file via PHP


$message->addAttachment('/path/to/file.pdf');
$message->addAttachment('report.docx', file_get_contents('/path/to/report.docx'));

// Attach Inline image

$message->setHtmlBody('

Hello

'); $message->addEmbeddedImage('/path/to/image.jpg', 'image_cid', 'image.jpg');

Custom Headers & Better Error Handling

Custom headers play a crucial role in email communication as they allow developers to add additional metadata or control to their messages. Software developers can add custom headers to their email messages using open source PHP-Mail library. Developers can use the addHeader() method, allowing for additional control over the email delivery. The library also provides better error handling mechanisms to handle exceptions that may occur during email sending operations. The following example shows how to add custom headers in an email messages using the PHP API.

Adding Custom Headers to Email Message using PHP Code

use Nette\Mail\Message;

$message = new Message();
$message->setFrom('sender@example.com')
        ->addTo('recipient@example.com')
        ->setSubject('Custom Headers Example')
        ->setBody('This is an email with custom headers.');

// Add custom headers
$message->addHeader('X-Custom-Header', 'Value');
$message->addHeader('X-Another-Header', 'Another Value');