PHP SMTP protokolü (RFC 5321) veya IMF (RFC 5322), ya da örneğin Python gibi MIME'yi uygulamıyor. Bunun yerine - PHP Hepsini sendmail MTA etrafında basit bir C sarıcı.
Ancak - tüm bu kadar sakıncaları ile - bir hala mim mesajları (parçalı / alternatif, / karma vb) oluşturmak ve html ve metin mesajları göndermek ve aynı zamanda varsayılan PHP'nin mail () fonksiyonu kullanarak dosyaları ekleyebilirsiniz. Sorun - kolay değil. "Mesaj" argüman için'' kurarken, "başlıkları" mail () argüman kullanarak, tüm mesajı handcrafting bitireceğiz. Ayrıca - posta () her yeni e-posta için yeni smtp bağlantı açar beri PHP'nin posta yoluyla bir döngü içinde e-posta göndermek () bir performans kaybı olacaktır.
/**sending email via PHP's Mail() example:**/
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Bu kısıtlamalar nedeniyle en millet gibi üçüncü parti kütüphaneleri kullanarak sonuna kadar:
- PHPMailer (download)
- Swiftmailer
- Zend_Mail
Bu kütüphaneleri kullanarak bir kolaylıkla metin veya html mesajlar oluşturabilirsiniz. Dosyaları ekleme de yapmak kolay bir şey olur.
/*Sending email using PHPmailer example:*/
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "from@example.com";
$mail->FromName = "Your Name";
$mail->AddAddress("myfriend@example.net"); // This is the adress to witch the email has to be send.
$mail->Subject = "An HTML Message";
$mail->IsHTML(true); // This tell's the PhPMailer that the messages uses HTML.
$mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML !";
$mail->AltBody = "Hello, my friend! \n\n This message uses HTML, but your email client did not support it !";
if(!$mail->Send()) // Now we send the email and check if it was send or not.
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent.';
}
ALSO:
Q: do i need smtp server on my host? can i just this in any free hosting?
A: any shared hosting has SMTP server nowadays (sendmail/postfix).