Build your professional network on facebook via our app Go to app
 
Topic : Email in PHP
  Rate : 
 
Industry : Internet Functional Area : Open Source
Keywords :

php

email

code

script

smtp

mime

Activity:  0 comments  269 views  last activity : 07 06 2010 20:18:04 +0000
Share
 
 
 

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

 
TrackBack URL:
0 comments on "Mime Formatted SMTP mail in PHP"
Add your comment on "Mime Formatted SMTP mail in PHP"

Rate:
Submit
JobSite for Investment Banking Professionals
"Service Delivery Manager" for Bangalore Location only female candidate we need
"Internal Quality Auditor" for Bangalore Location
Patch management Jobs bangalore
Viewers also viewed
PHP vs ASP.NET
 
0 referals 10 arguments, 2778 views
Obviously there is never a winner in “language X is better/faster/more scalable than language...
 
3 referals 33 arguments, 53425 views
Go to http://www.php.net/downloads.php and download the current version. PECL modules :...
40 referals 14 comments, 1003 views
more...  
Recent Knowledge (74)
In many ways, a manager has to be a leader, so therefore a manager will have many of the traits...
 
58 referals 24 comments, 319 views
The English language has some wonderfully anthropomorphic collective nouns for the various...
 
87 referals 5 comments, 82 views
After a cluster of disconcerting e-mails and documents surfaced last week from climate...
 
267 referals 5 comments, 67 views
more...  
More From Author
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.
more...