Build your professional network on facebook via our app Go to app
 
<< Prev  2 of 2 in Topic 
Topic : Email in PHP
  Rate : 
 
By : Anirban Bhattacharya, Team Leader -(Technical), Navayuga Infotech
Industry : Internet Functional Area : Open Source
Keywords :

php

email

code

script

smtp

mime

Activity:  0 comments  494 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
Leading Recruitment Firm
Jobs openings for WMB in Bangalore
Openings for Programmer Webfocus in Bangalore
Viewers also viewed
PHP vs ASP.NET
 
0 referals 17 arguments, 8007 views
Obviously there is never a winner in “language X is better/faster/more scalable than language...
 
3 referals 34 arguments, 73628 views
Internet is buzzing with reports that Facebook is all set to launch its GMail rival next week....
 
332 referals 57 arguments, 1181 views
more...  
Recent Knowledge (14)
It is relatively easy to identify the use count and resource usage of your SP’s, but first let...
244 referals 6 comments, 401 views
Military Strength: Comapre India and Pakistan Manpower and Ground Forces India has the second...
49 referals 9 comments, 67492 views
One young academically excellent person went for an interview for a managerial position in a big...
 
562 referals 38 comments, 1620 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...