Skip to content

Instantly share code, notes, and snippets.

@sercanarga
Created December 7, 2023 18:14
Show Gist options
  • Save sercanarga/a383870974cdc943de480cc6db1fa3e7 to your computer and use it in GitHub Desktop.
Save sercanarga/a383870974cdc943de480cc6db1fa3e7 to your computer and use it in GitHub Desktop.
smtp mail class
<?php
/**
* ArgaMailer Class
*
* Usage:
* $mailer = new ArgaMailer('smtp.server.com', 587, 'username', 'password');
* $mailer->connect();
* $mailer->login();
* $mailer->sendMail('from@example.com', 'to@example.com', 'Subject', 'Message');
* $mailer->quit();
*
* @package ArgaMailer
* @author Sercan Arga (mail@sercanarga.com)
* @version 1.0
*/
class ArgaMailer
{
private $smtpServer;
private $smtpPort;
private $username;
private $password;
private $smtpConnection;
public function __construct($smtpServer, $smtpPort, $username, $password) {
$this->smtpServer = $smtpServer;
$this->smtpPort = $smtpPort;
$this->username = $username;
$this->password = $password;
}
public function connect() {
$this->smtpConnection = fsockopen($this->smtpServer, $this->smtpPort, $err, $errstr, 3);
if (!is_resource($this->smtpConnection)) {
throw new Exception("SMTP sunucusuna bağlantı kurulamadı: $err - $errstr");
}
$response = $this->executeCommand("EHLO $this->smtpServer");
if (strpos($response, '220') === false) {
throw new Exception("EHLO komutu başarısız oldu: $response");
}
}
public function login() {
$response = $this->executeCommand("AUTH LOGIN");
if (strpos($response, '334') === false) {
throw new Exception("AUTH LOGIN komutu başarısız oldu: $response");
}
$response = $this->executeCommand(base64_encode($this->username));
if (strpos($response, '334') === false) {
throw new Exception("Kullanıcı adı doğrulama başarısız oldu: $response");
}
$response = $this->executeCommand(base64_encode($this->password));
if (strpos($response, '235') === false) {
throw new Exception("Şifre doğrulama başarısız oldu: $response");
}
}
public function sendMail($from, $to, $subject, $message) {
$response = $this->executeCommand("MAIL FROM: <$from>");
if (strpos($response, '250') === false) {
throw new Exception("MAIL FROM komutu başarısız oldu: $response");
}
$response = $this->executeCommand("RCPT TO: <$to>");
if (strpos($response, '250') === false) {
throw new Exception("RCPT TO komutu başarısız oldu: $response");
}
$response = $this->executeCommand("DATA");
if (strpos($response, '354') === false) {
throw new Exception("DATA komutu başarısız oldu: $response");
}
$headers = "From: $from\r\n";
$headers .= "To: $to\r\n";
$headers .= "Subject: $subject\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "\r\n";
$headers .= "$message\r\n";
$headers .= ".\r\n";
$response = $this->executeCommand($headers);
if (strpos($response, '250') === false) {
throw new Exception("Mail gönderilemedi: $response");
}
}
public function quit() {
$this->executeCommand("QUIT");
fclose($this->smtpConnection);
}
private function executeCommand($command) {
fputs($this->smtpConnection, "$command\r\n");
return fread($this->smtpConnection, 1024);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment