* @copyright 2011 Mesut Erdemir (http://mesuterdemir.com) * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version 1.0 * @link http://github.com/Mesut/SoruSor */ /* * * === KULLANIMI === * * $smtp = new Smtp("smtp sunucu adı", "smtp port(25/587)", "smtp kullanıcı adı(genelde mail adresi)", "smtp şifre(mail adresi şifresi)"); * * if($smtp->sendMail("gönderilecek adres", "mail konusu", "mail içeriği")) { * echo "gönderildi"; * } * else echo "gonderim basarisiz!"; * */ class Smtp { var $smtpServer; var $smtpPort = "25"; var $smtpUsername; var $smtpPassword; var $timeOut = "60"; var $clientIP = "127.0.0.1"; var $newLine = "\r\n"; /** * Constructor * * @param string $smtpServer * @param string $smtpPort * @param string $smtpUsername * @param string $smtpPassword * @return void */ public function Smtp($smtpServer, $smtpPort, $smtpUsername, $smtpPassword) { $this->smtpServer = $smtpServer; $this->smtpPort = $smtpPort; $this->smtpUsername = $smtpUsername; $this->smtpPassword = $smtpPassword; } /** * Send Mail Method * * @param string $mailTo * @param string $mailSubject * @param string $mailMessage * @return mixed */ public function sendMail($mailTo, $mailSubject, $mailMessage) { $smtpConnection = fsockopen($this->smtpServer, $this->smtpPort, $errno, $errstr, $this->timeOut); $smtpResponse = fgets($smtpConnection, 4096); // Check SMTP Connection if(empty($smtpConnection)) { $output = "Bağlantı sağlanamadı: $smtpResponse"; return $output; } // Connection to SMTP Server fputs($smtpConnection, "HELO {$this->clientIP}" . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // AUTH Login fputs($smtpConnection, "AUTH LOGIN" . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // Send Username for Authentication fputs($smtpConnection, base64_encode($this->smtpUsername) . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // Send Password for Authentication fputs($smtpConnection, base64_encode($this->smtpPassword) . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // Email From fputs($smtpConnection, "MAIL FROM: <{$this->smtpUsername}>" . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // Email To fputs($smtpConnection, "RCPT TO: <$mailTo>" . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // Email Body fputs($smtpConnection, "DATA" . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); // Construct Headers $headers = "MIME-Version: 1.0" . $this->newLine; $headers .= "Content-type: text/html; charset=utf-8" . $this->newLine; $headers .= "Content-Transfer-Encoding: base64" . $this->newLine; $headers .= "To: <$mailTo>" . $this->newLine; $headers .= "From: <{$this->smtpUsername}>" . $this->newLine; // Send Message via SMTP Connection $mailMessage = base64_encode($mailMessage); fputs($smtpConnection, "Subject: $mailSubject\r\n$headers\r\n\r\n$mailMessage\r\n.\r\n"); $smtpResponse = fgets($smtpConnection, 4096); // Close SMTP Connection fputs($smtpConnection, "QUIT" . $this->newLine); $smtpResponse = fgets($smtpConnection, 4096); $quitCode = substr($smtpResponse, 0, 3); fclose($smtpConnection); // If return code is 221, OK if ($quitCode == 221) { return true; } else return false; } } ?>