Free PHP API to Send Email Messages with Attachments
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.
What is PHP-Mail Library?
For PHP developers seeking a powerful yet elegantly simple solution for email functionality, PHP-Mail from the Nette framework stands out as a premier library for composing and sending emails. This expertly crafted tool transforms email integration from a complex task into a streamlined process, offering a user-friendly API that requires only a few lines of code to generate and dispatch professional messages. It is packed with essential capabilities for modern applications, including robust support for multi-part messages in both HTML and plain text formats, seamless file attachment handling, and the ability to embed images directly into email content. With built-in support for multiple transport methods like SMTP and Sendmail, along with features for custom email headers and improved error handling, the library provides a comprehensive and reliable foundation for any email-sending need.
The true strength of the Nette Mail PHP Library lies in its masterful balance of simplicity and advanced functionality, designed to streamline email operations for software developers. Its intuitive design and easy integration possibilities allow for rapid implementation, enabling developers to focus on core application logic rather than email configuration. Beyond basic sending, the library offers sophisticated options such as scheduling emails for later delivery, granting greater control over communication workflows. This combination of a straightforward API, versatile transport options, and powerful features like in-line image embedding and custom header management makes it an indispensable tool. By simplifying complex tasks and offering extensive customization, the PHP-Mail library empowers developers to build robust, efficient, and highly functional email features directly into their PHP applications with minimal effort.
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/mailYou 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.
How to Add Custom Headers to Email Message using PHP Library?
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');