🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 2 Min Lesezeit
0

Mail Service using Singleton Design Pattern in PHP

↗ Quelle (dev.to)
🗣️ Stimme:

Today, I am implementing a Mail Service class using the Singleton Design Pattern in PHP. In many applications, there are frequent email-sending operations, like on an e-commerce website, where an email is sent to the user after they place an order.



Using a Singleton pattern here allows the application to create only a single instance of the mail service connection. This enhances application performance by ensuring only one connection instance is maintained.



Please see the code below:




CODE
    // Chúng ta có thể cấu hình namespace cho ok hơn, vì mình test nên để require_once đến thư viện
require_once "../../../vendor/autoload.php";
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class MailService{
private static $instance = null;
private $transport = null;
private $mail_from = ""; // email
private $pass = ""; // password
private $mailer = null;
private function __construct(){
try {
$this->transport =Transport::fromDsn("smtp://{$this->mail_from}:{$this->pass}@smtp.gmail.com:587?encryption=tls");

$this->mailer = new Mailer($this->transport);
} catch (\PDOException $e) {
throw new Exception($e->getMessage());
}
}

public static function getInstance(){
if(self::$instance === null){
// begin create instance
self::$instance = new MailService();
}
// create instance object
return self::$instance;

}

public function sendEmail(Array $data) : Array{
try {
$email = new Email();
$email->from($this->mail_from);
$email->to($data['email']);
$email->subject($data['subject']);
$email->text($data['message']);
if($data['file'] != null){
$email->attachFromPath($data['file']);
}
$this->mailer->send($email);
$data['status'] = 'success';
return json_decode(json_encode($data), true, 512, JSON_THROW_ON_ERROR);
}
catch (\Exception $e) {
$data['status'] = 'error';
$data['message'] = $e->getMessage();
return json_decode(json_encode($data), true, 512, JSON_THROW_ON_ERROR);
}

}

}







Great! Now that the Singleton pattern is set up for the Mail Service, we can use it in our application. Here’s how you can integrate and use the Mail Service Singleton:




CODE
$mail = MailService::getInstance();
$mail2 = MailService::getInstance();

var_dump($mail === $mail2); //true
$mail->sendEmail([
'subject' => 'LẬP TRÌNH WEBSITE | HOANGUYENIT',
'email' => '[email protected]',
])







"You can connect with me on , or my Facebook Fanpage"

"I always share programming knowledge there, so feel free to follow me if you're interested!"

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Mail Service using Singleton Design Pattern in PHP

Thematisch verwandte Begriffe: Mail, Service, using, Singleton · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...