A hacker doesn't really need PHP to send an Email. He needs only a command prompt and a telnet program, that's all. But we are in the PHP Zone, so we will do it with PHP too. All the stuff here is for learning purpose. Don't use it to hack or to spam or to do any other illegal action.
First, a good start is to read the SMTP specification protocol. It helps to understand how things work. You can read the Request For Comments number 2821 document (RFC2821) for that. It's a good idea to read the entire document, but if you haven't the time, just read the chapter 4.
So, let's first do it with a shell prompt and a telnet program and then write the PHP code. In windows, click Start->Run and type cmd.exe to open the command prompt. In a nix environment, guess what :-)
We connect to the SMTP server:
Change smtp.mydomain.com with the address of your SMTP server. The 25 is the port number on which the SMTP server listens. If you are connected, you should get a string that starts with 220 as a response and you have to introduce your self with the HELO command:
No matter what you give as argument to the HELO command, most SMTP servers will show you your real address and your IP in a response to this command. If all is ok, you will get a string that starts with 250.
Let's now quickly finish this session:
OK, we are done and the email is sent.
Lines 2, 4, 6, 18 and 20 are the server responses.
Line 1: Enter the address of the sender (required). Pay attention to "<" and ">". Don't forget them.
Line 3: Enter the address of the receiver (required). Pay attention to "<" and ">". Don't forget them.
Line 5: We say to the server: now, we start the message to send (required)
Lines 7 to 11: We write some headers (Not required)
Lines 12 to 16: The message we want to send
Lines 16-17: empty line and a point: that's how to finish and send the email. Look at the response of the server at Line 6.
Note about Lines 7 to 11:
This is what the receiver gets in the email header. So, if you want to send anonymous mail (don't do it. It's easy to TraceRoute back your email and get to you) or if you want to have fun with your friend, you can set his email address in the "From" field. He will get an email sent from him to him. For fun also, you can set in the "Date" field a date in the future. You friend will receive an email from the future.
Let's write the PHP code now:
To connect to the server:
The fsocketopen will time out after 30 seconds if the connection failed. The $errorno and $errstr will contain the error number and the error message if an error happens.
To get the server response use the fgets function like this:
I'll not care about the server responses here; I want just to send the email. If you want to write a good program you should care.
To send a string to the server use fwrite function:
IMPORTANT: all commands to be sent to server must end with "\r\n" : CR + LF
Here is the code (no error handling here too):
That's all. The email is sent.
Here is the full script:
This script can be improved to send mails with attachments. You need only to code a base64 coding function. The algorithm for base64 coding is very simple: from 3 bytes we construct 4 bytes. May be I'll write something about it one day.
Conclusion:
You can always get a PHP class from out there easy to work with and hides all this stuff, but the hacker way is to do things by hand with the minimum and simplest tools like a command prompt or a simple text editor.
First, a good start is to read the SMTP specification protocol. It helps to understand how things work. You can read the Request For Comments number 2821 document (RFC2821) for that. It's a good idea to read the entire document, but if you haven't the time, just read the chapter 4.
So, let's first do it with a shell prompt and a telnet program and then write the PHP code. In windows, click Start->Run and type cmd.exe to open the command prompt. In a nix environment, guess what :-)
We connect to the SMTP server:
telnet smtp.mydomain.com 25
Change smtp.mydomain.com with the address of your SMTP server. The 25 is the port number on which the SMTP server listens. If you are connected, you should get a string that starts with 220 as a response and you have to introduce your self with the HELO command:
HELO its_me
No matter what you give as argument to the HELO command, most SMTP servers will show you your real address and your IP in a response to this command. If all is ok, you will get a string that starts with 250.
Let's now quickly finish this session:
1: MAIL FROM:<me@mydomain.com>
2: 250 OK
3: RCPT TO:<myfreind@hisdomain.com>
4: 250 OK
5: DATA
6: 354 Start mail input; end with <CRLF>.<CRLF>
7: Received: from mydomain.com by hisdomain.com ; Thu, 03 Jan 2006 12:33:29 -0700
8: Date: Thu, 03 Jan 2006 12:33:22 -0700
9: From: Me <me@mydomain.com>
10: Subject: The Next Meeting of the Board
11: To: myfreind@hisdomain.com
12:
13: MyFreind:
14: How are you?
15:
16: Me.
17: .
18: 250 OK
19: QUIT
20: 221 mydomain.com Service closing transmission channel
OK, we are done and the email is sent.
Lines 2, 4, 6, 18 and 20 are the server responses.
Line 1: Enter the address of the sender (required). Pay attention to "<" and ">". Don't forget them.
Line 3: Enter the address of the receiver (required). Pay attention to "<" and ">". Don't forget them.
Line 5: We say to the server: now, we start the message to send (required)
Lines 7 to 11: We write some headers (Not required)
Lines 12 to 16: The message we want to send
Lines 16-17: empty line and a point: that's how to finish and send the email. Look at the response of the server at Line 6.
Note about Lines 7 to 11:
This is what the receiver gets in the email header. So, if you want to send anonymous mail (don't do it. It's easy to TraceRoute back your email and get to you) or if you want to have fun with your friend, you can set his email address in the "From" field. He will get an email sent from him to him. For fun also, you can set in the "Date" field a date in the future. You friend will receive an email from the future.
Let's write the PHP code now:
To connect to the server:
$smtp_server = fsockopen("smtp.mydomain.com", 25, $errno, $errstr, 30); The fsocketopen will time out after 30 seconds if the connection failed. The $errorno and $errstr will contain the error number and the error message if an error happens.
if(!$server_smtp)
{
// we have an error, do something
exit;
}
To get the server response use the fgets function like this:
$server_response = fgets($smtp_server);
I'll not care about the server responses here; I want just to send the email. If you want to write a good program you should care.
To send a string to the server use fwrite function:
fwrite($smtp_server, $command);
IMPORTANT: all commands to be sent to server must end with "\r\n" : CR + LF
Here is the code (no error handling here too):
fwrite($smtp_server, "HELO its_me\r\n");
fwrite($smtp_server, "MAIL FROM:<me@mydomain.com>\r\n");
fwrite($smtp_server, "RCPT TO:<myfreind@hisdomain.com>\r\n");
fwrite($smtp_server, "DATA\r\n");
fwrite($smtp_server, "Received: from mydomain.com by hisdomain.com ; Thu, 03 Jan 2006 12:33:29 -0700\r\n");
fwrite($smtp_server, "Date: Thu, 03 Jan 2006 12:33:22 -0700\r\n");
fwrite($smtp_server, "From: Me <me@mydomain.com>\r\n");
fwrite($smtp_server, "Subject: The Next Meeting of the Board\r\n");
fwrite($smtp_server, "To: myfreind@hisdomain.com\r\n");
fwrite($smtp_server, "\r\nMyFreind:\r\nHow are you ?\r\n\r\n Me.\r\n");
fwrite($smtp_server, ".\r\nQUIT\r\n");
That's all. The email is sent.
Here is the full script:
<?php
$smtp_server = fsockopen("smtp.mydomain.com", 25, $errno, $errstr, 30);
if(!$server_smtp)
{
// We have an error, do something
exit;
}
fwrite($smtp_server, "HELO its_me\r\n");
fwrite($smtp_server, "MAIL FROM:<me@mydomain.com>\r\n");
fwrite($smtp_server, "RCPT TO:<myfreind@hisdomain.com>\r\n");
fwrite($smtp_server, "DATA\r\n");
fwrite($smtp_server, "Received: from mydomain.com by hisdomain.com ; Thu, 03 Jan 2006 12:33:29 -0700\r\n");
fwrite($smtp_server, "Date: Thu, 03 Jan 2006 12:33:22 -0700\r\n");
fwrite($smtp_server, "From: Me <me@mydomain.com>\r\n");
fwrite($smtp_server, "Subject: The Next Meeting of the Board\r\n");
fwrite($smtp_server, "To: myfreind@hisdomain.com\r\n");
fwrite($smtp_server, "\r\nMyFreind:\r\nHow are you ?\r\n\r\n Me.\r\n");
fwrite($smtp_server, ".\r\nQUIT\r\n");
?>
This script can be improved to send mails with attachments. You need only to code a base64 coding function. The algorithm for base64 coding is very simple: from 3 bytes we construct 4 bytes. May be I'll write something about it one day.
Conclusion:
You can always get a PHP class from out there easy to work with and hides all this stuff, but the hacker way is to do things by hand with the minimum and simplest tools like a command prompt or a simple text editor.
30 Jan 2006 02:55:09
Thanks Mustapha. The tutorial is very valuable.
09 Feb 2006 02:10:09
thanks i read it all,an found it to be very useful.lol
01 Mar 2006 20:47:16
hehehe... This is a lovely "educational" tutorial. haha.
08 Mar 2006 16:30:31
and what about ssl connection?
13 Mar 2006 04:06:20
be care bro! ..nice tutorials
18 Mar 2006 23:03:45
COOL MAKE MORE
20 Mar 2006 16:03:44
this is a realy good tutorial, I like it man!
22 Mar 2006 03:13:21
Thanks to you !!
08 Aug 2006 14:41:28
Thanks It help me to solve my problem.
06 Oct 2006 08:05:04
Sorry...it's not working
Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.yahoo.com:25 (Connection refused) in /home/wwwpoke/public_html/cektime/test.php on line 3
can you help me ?
pls send the code to aldo_q2001@yahoo.com
10 Oct 2006 04:13:26
To Aldo:
A little bit of Googling reveals that Yahoo's SMTP server is at smtp.mail.yahoo.com.
Directly from Yahoo.com:
<i>If your ISP blocks port 25 or if you're unable to send email, then you will need to use port 587 when sending via Yahoo!'s SMTP server.</i>
Try that.
At the article:
Good article. I remember messing around with MTA's such as Kerio, only experiencing difficulties.
This solution is so much easier and nicer to work with and makes finishing my work a lot easier!
26 Oct 2006 14:42:07
can u help me!!! i need it urgently
wen im usin sockets i got error in authentication i dont undertstand dat part
pleeez help do i need any authentication to connect smtp? for yahoo,gmail?
pleeeeeez its urgent mail me if u know
28 Nov 2006 18:44:19
when i tried to send mail with command prompt
telnet smtp.mail.yahoo.com 587
mail from:<blog_seo@yahoo.com>
HERE I GOT FOLLOWING ERRIR
530 authentication required - for help go to http://help.yahoo.com/help/...
pop/pop-11.html
Connection to host lost.
PLS HELP AND THANX FOR NICE ARTICLE REALLY INTERESTING
27 Jan 2007 17:31:07
Hi, thanks!
04 Apr 2007 13:56:40
the tutorial rocks man.
But what if it cannot connect to the smtp server?
i wrote a script that connects to the smtp server, if it's runned on my local apache server, but if it's runned on the main domain(the place where i eventually put the script) i have an error on the fsockopen function:
fsockopen() [function.fsockopen]: unable to connect to xxx.xxx.xxx.xxx:25 (Connection refused) in /home/btechro/public_html/rezervari/Net/Socket.php on line 138
What shoul i do ?
27 Apr 2007 00:20:06
mail($email,ACCOUNT_CONFIRM,ACCOUNT_MSG."\n\n".$BASEURL."/account.php?act=confirm&confirm=$random&language=$idlangue","From: $SITENAME <$SITEEMAIL>");
my outgoing server requires authentication and i keep getting this error:
Warning: mail() [function.mail]: SMTP server response: 530 Authentication required in C:\xampp\htdocs\bt\account.php on line 611
am i missing something??
10 May 2007 16:24:24
Everyone, the example given at this website is meant to be just that. AN EXAMPLE! It does a good job of showing the use of fsockopen() in a basic example. It is not, however, useful in real world use. While the author asks you to first read the SMTP specification, his code snippet doesn't account for SMTP communication which is two way! Request - Response. If true, Request - Response. Etc. If you really want to interact with an SMTP server (including SMTP-AUTH) I'd suggest using class.smtp.php from
http://phpmailer.sourceforg...
Otherwise you will never have reliable SMTP communication with any internet mail server. Period.
09 Jun 2007 05:37:34
Hey, I am experiencing a serious issue with an email from someone who is hacker....someone is able to use my email and sent a dirty message to one of my friend and cause my friend to think it is from me...but actually it is not true !! Someone has sucessfully used my email without any traces....I looked at my friend's email and all I see is that it was sent by my email nothing other email only mine. So is that possible for hacker to use someone's email and sent it without trace knowing that hacker actually did it not the email's owner ? If that is true...can you tell me how do I prevent it and how to trace that bitch hacker ???
Thanks !!
09 Jun 2007 17:46:40
Adi 4,
Yes it is possible and very easy to do. Read again the "Note about Lines 7 to 11" section.
To trace it back is not easy but it is possible. See the full header of the email if it contains a messageID. if so, it contains a very valuable infos. Check the received-by sequences and read them from top to bottom. The last received-by line contains the first server used by the hacker. So it is either his Own computer or the server of his internet provider.
But from your description, I think it is someone who knows both you and your freind :-)
02 Jul 2007 12:34:42
this code doesnt work..
$smtp_server = fsockopen("smtp.mail.yahoo.com", 587, $errno, $errstr, 30);
what is the value of $errno and $errstr?
hey pls help me... or email me @ y0h_azakura87@yahoo.com also my ymid...
thanks
29 Dec 2007 12:52:08
for getting smtp server address it is usefull to use an mx dns lookup, which you can perform directly thru some system command, or parse response of http://maxban.com/cgi-bin/s... (replace blabla.com with your domain)
20 Jan 2008 15:57:32
how do i add multiple address in to field???? please help me in this regard..
20 Mar 2008 11:03:32
i want to send a friend request to a buddy and after sending it should show the message"Request sent"
And on other side when tht buddy logs in he should be able to recieve tht request with "Accept" and"Reject" button with accepting the request and rejecting the request respctively..
on accepting the name should get added in the list and on rejecting it should get added..
I hope u understud my problem..
I have been searching over net..but couldnt get any sourcecode or function tht deals with it..
plz... do help me solve this problem..by givng me an appropriate code..
17 Jul 2008 13:39:35
How can one we set the MIME to be HTML ?
09 Sep 2008 07:24:59
Hi there! It is a great tutorial and easy to follow. However, I could not get it to work in php code, but it works in command prompt.Please advise.