| Topic : Email in PHP |
|
|
|
|
||
|
Source : http://www.wertex.org
Activity:
0 comments
494 views
last activity : 07 06 2010 20:18:04 +0000
|
||
|
|
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

|
|
|
|
|
|
|
|
|
|
|
|
Waterfall is the thing of the past. Agile is always better because 1. Each cycle is short. You have enough scope to fix a problem. 2. Each target is short and thereby achievable. 3. One shortfall in any cycle can be solved in the next cycle .. And... |
You should not jump into action after receiving the customer complaint. The word complaint is negative. Take it as feedback and not complaint. In my opinion you should proceed in this way. 1. Receive complaint 2. Response to customer that you are... |
Just Zero on some big job sites like Naukri, Monster and do regular search. Keep your profile updated and keep contact with all your friends. That's it. |