A lightweight and efficient email handling library for PHP, providing SMTP, IMAP, and POP3 support.
Install the package via Composer:
composer require zeus/emailor updated code
composer require zeus/email:dev-mainSet up the SMTP authenticator:
<?php
use Zeus\Email\CommandSender;
use Zeus\Email\SmtpAuthenticator;
require_once 'vendor/autoload.php';
$smtpAuthenticator = new SmtpAuthenticator(
"sandbox.smtp.mailtrap.io",
"your_username",
"your_password",
2525,
10,
[
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
]
]
);
Mail::setSmtpAuthenticator($smtpAuthenticator); // Set it onceDefine your email class:
class WelcomeMail implements EmailFactoryInterface
{
public function build(EmailBuilder $builder): void
{
$builder->setSubject("Welcome to Our Website");
$builder->setHtmlBody('<html><body><h1>Welcome!</h1><p>This is a test email with an attachment.</p></body></html>');
$builder->addCc('cc@example.com');
$builder->addBcc('bcc@example.com');
$builder->setReplyTo('berxudar@gmail.com');
$builder->addAttachment(__FILE__);
}
}$response = Mail::to('aslan@gmail.com')
->from('berxudar@gmail.com')
->send(new Payment());
if ($response) {
echo "β
Email sent successfully";
} else {
echo "β Failed to send email";
}and extra features
class Logger implements \Zeus\Email\EmailLogInterface
{
public function log(string $message,int $level)
{
//log operations
}
}
$mail = Mail::to('aslan@gmail.com');
$mail->from('berxudar@gmail.com')
->logTo(new Logger())
->beforeSend(function (EmailBuilder $builder) {
$builder->addCc('test@gmail.com');
})
->afterSend(function () {
//trigger an event
})
->send(new Payment());The scheduleAt method allows you to schedule an email to be sent at a specific date and time in the future. It accepts a DateTime object that specifies when the email should be sent, allowing you to automate the sending process based on your requirements.
To use scheduleAt, call it within the email building process and pass a valid DateTime instance as an argument. This will queue the email to be sent at the specified date and time.
class Payment implements EmailFactoryInterface
{
/**
* @throws Exception
*/
public function build(EmailBuilder $builder): void
{
$builder->setSubject("Welcome to Our Website");
$builder->setBody('Test a message');
$builder->scheduleAt(new DateTime('last day of this month'));
}
}In this example, the email will be scheduled to be sent on the last day of the current month. You can adjust the DateTime object to any valid date and time format.
- DateTime $datetime: The exact date and time when the email should be sent. It accepts any valid PHP
DateTimeformat.
You can set the email to send on a specific date like this:
$builder->scheduleAt(new DateTime('2025-04-01 09:00:00'));This will schedule the email to be sent on April 1st, 2025 at 9:00 AM.
- Make sure to have a scheduling system (such as cron jobs or a task queue) in place to handle email sending at the scheduled time.
- If the
DateTimeobject provided is invalid, an exception may be thrown. - The method does not send the email immediately but queues it for future delivery.
Δ°Εte setDelay metodunun tanΔ±tΔ±mΔ±nΔ± iΓ§eren README formatΔ±nda yazΔ±lmΔ±Ε metin:
The setDelay method allows you to delay the sending of an email by a specific amount of time. This method accepts a Delay object that lets you add hours, minutes, and seconds to the email's send time, making it easy to schedule emails to be sent after a delay.
To use setDelay, simply call it within the email building process and pass a valid Delay object as an argument. You can chain methods like addHour(), addMinute(), or addSecond() to specify the delay duration.
class Payment implements EmailFactoryInterface
{
/**
* @throws Exception
*/
public function build(EmailBuilder $builder): void
{
$builder->setSubject("Welcome to Our Website");
$builder->setBody('Test a message');
$builder->setDelay(new Delay()->addHour(1)->addMinute(15));
}
}of for debugging with the dd method
class Payment implements EmailFactoryInterface
{
/**
* @throws Exception
*/
public function build(Emai
9D65
lBuilder $builder): void
{
$builder
->setSubject("Welcome to Our Website")
->setBody('Test a message');
->setDelay(new Delay()->addHour(1)->addMinute(15))
->dd();
}
}In this example, the email will be delayed for 1 hour and 15 minutes before being sent. You can adjust the delay time by chaining additional addHour(), addMinute(), or addSecond() methods to customize the delay duration.
- Delay $delay: A
Delayobject that specifies the duration to delay the email.- Use
addHour($hours)to add hours to the delay. - Use
addMinute($minutes)to add minutes to the delay. - Use
addSecond($seconds)to add seconds to the delay.
- Use
$builder->setDelay(new Delay()->addHour(2)->addMinute(30));This will delay the email by 2 hours and 30 minutes before sending it.
The BulkReceiver class is designed to make it easy to send an email to multiple recipients at once. It allows you to add multiple email addresses and manage them as a group. This is particularly useful when sending bulk emails such as newsletters, notifications, or promotional content.
To use the BulkReceiver class, you need to create an instance of it and add each recipient with the addReceiver() method. You can optionally specify a name for each recipient.
class X implements EmailFactoryInterface
{
public function build(EmailBuilder $builder): void
{
$builder
->setSubject('Test Email')
->setBody('This is a test email');
}
}
$bulkReceiver = new BulkReceiver();
$bulkReceiver->addReceiver('john@example.com', 'john');
$bulkReceiver->addReceiver('jane@example.com', 'jane');
$bulkReceiver->addReceiver('jim@example.com', 'jim');
$mail = Mail::to($bulkReceiver)
->from('berxudar@gmail.com');
$mail->send(new X());-
addReceiver(string $email, string $name = null): Adds a recipient to the list. The
$emailis required, and you can optionally specify a$name(like "John" or "Jane") for personalized addressing.$bulkReceiver->addReceiver('john@example.com', 'john');
-
to(BulkReceiver $bulkReceiver): Pass the
BulkReceiverinstance to theto()method to set the list of recipients.$mail = Mail::to($bulkReceiver);
$bulkReceiver = new BulkReceiver();
$bulkReceiver->addReceiver('john@example.com', 'John Doe');
$bulkReceiver->addReceiver('jane@example.com', 'Jane Smith');
$bulkReceiver->addReceiver('jim@example.com', 'Jim Brown');-
Multiple Recipients: You can add as many recipients as you need. The
BulkReceiverclass can handle large recipient lists efficiently. -
Custom Names: Adding custom names is optional, but it allows for personalized communication (e.g., in the "To" field of the email).
-
Sending Emails: The
BulkReceiverclass is passed into theMail::to()method to manage the recipients for your email. -
Scalability:
BulkReceiveris ideal for sending bulk emails to multiple recipients at once without the need to manually set each recipient.
This project is licensed under the MIT License. See the LICENSE file for details.
- Dilo Surucu (Abdulkadir) - GitHub