php mail fonksiyonu ile mail göndermek

Şubat 27, 2009 | In: Anasayfa, Php

Php dosyalarınızda mail gönderirken kullanabileceğiniz php mail fonksiyonunun yanında mail sunucularına bağlanıp fputs yöntemi ilede mail gönderebilirsiniz. Aşağıda bununla ilgili bazu fonksiyon örnekleri bulabileceksiniz.

Php mail fonksiyonunun kullanımı

$Kimden = “Gönderenin Adı”; //gönderici
$email = “email@mailadresi.com”;
//gönderici e-posta adresi
$alici = “alici@mailadresi.com”;
//alıcı
$metin = “Mesajınız buraya gelecek…”;
//mail içeriği
$konu = “Mesaj Başlığı”;
//subject
$header= “From: “. $Kimden . ” <” . $email . “>\r\n”;
//header bilgisi
$header.= “Content-Type: text/plain; charset:iso-8859-9\r\n”; //header bilgisi

mail($alici, $konu, $mesaj, $header); //mail command :)

Eğer bu fonksiyonu kullanarak gönderdiğiniz maillerin sisteme spam olarak düştüğünü görürseniz, veya daha teknik bir metod denemek isterseniz aşağıdaki yöntem sizin için bir çözüm olacaktır.
function posta($to, $subject, $message)
 {
 $from=”test@test.com.tr“;
 $namefrom=”Test Maili”;
 $nameto=$to;
 
 /*  your configuration here  */
 
 $smtpServer = “192.168.0.1″; //ip accepted as well
 $port = “25″; // should be 25 by default
 $timeout = “10″; //typical timeout. try 45 for slow servers
 $username = “test”; //the login for your smtp
 $password = “password”; //the pass for your smtp
 $localhost = “192.168.0.1″; //this seems to work always
 $newLine = “\r\n”; //var just for nelines in MS
 $secure = 0; //change to 1 if you need a secure connect
  
 /*  you shouldn’t need to mod anything else */
 
 //connect to the host and port
 $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
 $smtpResponse = fgets($smtpConnect, 4096);
 if(empty($smtpConnect))
 {
    $output = “Failed to connect: $smtpResponse”;
    return $output;
 }
 else
 {
    $logArray['connection'] = “Connected to: $smtpResponse”;
 }
 
 //say HELO to our little friend
 fputs($smtpConnect, “HELO $localhost”. $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['heloresponse'] = “$smtpResponse”;
 
 //start a tls session if needed
 if($secure)
 {
    fputs($smtpConnect, “STARTTLS”. $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['tlsresponse'] = “$smtpResponse”;
 
    //you have to say HELO again after TLS is started
    fputs($smtpConnect, “HELO $localhost”. $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['heloresponse2'] = “$smtpResponse”;
 }
 
 //request for auth login
 fputs($smtpConnect,”AUTH LOGIN” . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['authrequest'] = “$smtpResponse”;
 
 //send the username
 fputs($smtpConnect, base64_encode($username) . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['authusername'] = “$smtpResponse”;
 
 //send the password
 fputs($smtpConnect, base64_encode($password) . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['authpassword'] = “$smtpResponse”;
 
 //email from
 fputs($smtpConnect, “MAIL FROM: $from” . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['mailfromresponse'] = “$smtpResponse”;
 
 //email to
 fputs($smtpConnect, “RCPT TO: $to” . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['mailtoresponse'] = “$smtpResponse”;
 
 //the email
 fputs($smtpConnect, “DATA” . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['data1response'] = “$smtpResponse”;
 
 //construct headers
 $headers = “MIME-Version: 1.0″ . $newLine;
 $headers .= “Content-type: text/html; charset=iso-8859-9″ . $newLine;
 $headers .= “To: $nameto <$to>” . $newLine;
 $headers .= “From: $namefrom <$from>” . $newLine;
 
 //observe the . after the newline, it signals the end of message
 fputs($smtpConnect, “To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n”);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['data2response'] = “$smtpResponse”;
 
 // say goodbye
 fputs($smtpConnect,”QUIT” . $newLine);
 $smtpResponse = fgets($smtpConnect, 4096);
 $logArray['quitresponse'] = “$smtpResponse”;
 $logArray['quitcode'] = substr($smtpResponse,0,3);
 fclose($smtpConnect);
 //a return value of 221 in $retVal["quitcode"] is a success
 return($logArray);
}

Bu fonksiyonu kullandığınızda gönderdiğiniz e-postalar sunucu tarafından bir mail clientle gönderilmiş gibi işlem görecektir.



3 Responses to php mail fonksiyonu ile mail göndermek

Avatar

destny

Nisan 23rd, 2009 at 19:52

thanks=)

Avatar

hakan yıldızzz

Mayıs 29th, 2009 at 13:42

abiler ben bi yıllık ödev aldım php den.ödevim=öğrenci not giriş sistemi ben bunu nasıl yapabilirim biraz zorlanıyorum da yardım edrmsnzzz lütfenn

Avatar

Mustafa

Haziran 10th, 2009 at 22:13

İkinci gönderim yöntemi, fsocket ile olanı, aslında kesin çözüm gibi. Bunun için epey önceden bir sınıf yazmıştım. Asıl amacım kolayca ek dosyaları da gönderebilmekti.

Sebebini anlayamadığım tek şey ise, birkaç saniye de olsa php’nin mail() fonksiyonuna göre biraz yavaş işlemesiydi.

Comment Form

Categories