MIME Formatted SMTP Mail in PHP - A Tutorial
Here is a quick tutorial which will help you to learn writing MIME formatted SMTP Mail
SMTP stands for Simple Mail Transfer Protocol and it works using Port 25. The Script first connects with SMTP server and then sends headers and body to the SMTP server which then relays it to recipient.
$connect = fsockopen ($smtp_server, $port, $errno, $errstr, 30);
The above statement connects to SMTP server by opening socket. $smtp_server is generally localhost or 127.0.0.1 or the server name or IP designated by your system admin. $port is usually 25. $errno, $errstr generally stores the error number and error type if any error encountered during connect. Once you are connected the next step is to greet the SMTP server with HELO command.
if (!$connect) return false;
$rcv = fgets($connect, 1024);
fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n");
$rcv = fgets($connect, 1024);
$rcv is storing the response while you are sending command using fputs function. The typical response of HELO command is EHELO.
Now the series of headers and command
fputs($connect, "RSET\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "MAIL FROM:$fromemail\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "RCPT TO:$toemail\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "DATA\r\n");
$rcv = fgets($connect, 1024);
$boundary = time()."Some text";
Every MIME formatted email uses a boundary which marks boundary between various content-type. The boundary should be unique. So we are using time along with some texts. Every boundary that separates content type are written as –boundary. Like when you are sending an email which sends both HTML and plain text, then the first content type will be multipart/alternative and each part shall be separated using a boundary. The boundary finishes with –boundary–.
fputs($connect, "Subject: $subject\r\n");
fputs($connect, "From: $fromname <$fromemail>\r\n");
fputs($connect, "To: $toemail\r\n");
fputs($connect, "X-Sender: <$fromemail>\r\n");
fputs($connect, "Return-Path: <$fromemail>\r\n");
fputs($connect, "Errors-To: <$fromemail>\r\n");
fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $fromname)."@$smtp>\r\n");
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "X-Mailer: PHP - $fromMailer\r\n");
fputs($connect, "X-Priority: 3\r\n");
fputs($connect, "Date: ".date("r")."\r\n");
fputs($connect, "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n\r\n");
fputs($connect, "--$boundary\r\n\r\n");
fputs($connect, "Content-Type: text/plain; charset=US-ASCII\r\n");
fputs($connect, "Content-Transfer-Encoding: quoted-printable\r\n");
fputs($connect, "\r\n\r\n");
fputs($connect, $plain."\r\n\r\n");
fputs($connect, "--$boundary\r\n\r\n");
fputs($connect, "Content-Type: text/Richtext; charset=$charset\r\n");
fputs($connect, "Content-Transfer-Encoding: quoted-printable\r\n");
fputs($connect, "\r\n\r\n");
fputs($connect, $body."\r\n\r\n");
fputs($connect, "--$boundary\r\n");
fputs($connect, "Content-Type: text/html; charset=\"$charset\"\r\n");
fputs($connect, "Content-Transfer-Encoding: 8bit\r\n");
fputs($connect, "\r\n\r\n");
fputs($connect, $body."\r\n\r\n");
fputs($connect, "--$boundary--\r\n");
fputs($connect, "\r\n");
fputs($connect, "\r\n.\r\n");
$rcv = fgets($connect, 1024);
fputs ($connect, "QUIT\r\n");
$rcv = fgets ($connect, 1024);
fclose($connect);
I hope this code will help you all





