PHP has a special function for mailing – mail(), but in some cases PHP script to send email using SMTP authentication has great potential and can be used for this purpose.

1. The main reason for using the php script is the problem of antispam filters

Modern servers are configured in such a way that mail () function adds information, indicating that the mail was sent using mail() to the title of the letter. As usual anti-spam filters block such letters. If the filter is hard-coded, then most likely, it will remove the letter on the receiver side. You can avoid such titles using the server settings (if you have access to server configuration), but it may take you a lot of time and is not very easy.

2. Specific reason.
You can mask the letter like it was not sent by the mailing program.

How to write the php script to send email using SMTP?
After each command transmission to the server you have to receive an answer from the server. To do this make a function that will work with an open connection and get these answers.

<?

function get_data($smtp_conn)

{

$data="";
while($str = fgets($smtp_conn,515))
{
$data .= $str;
if(substr($str,3,1) == " ") { break; }
}
return $data;
}
?>

Create the titles:

<?
$header="Date: ".date("D, j M Y G:i:s")." +0700\r\n";
$header.="From: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('test_1')))."?= <login@domain.ru>\r\n"; //, где Windows-1251 - encoding, test_1 - the name of the sender, login@domain.ru - the e-mail to send from

$header.="X-Mailer: The Bat! (v3.99.3) Professional\r\n";
$header.="Reply-To: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('test_1')))."?= <login@domain.ru>\r\n"; //, где Windows-1251 - encoding, test_1 - the name of the sender,
login@domain.ru - the e-mail to send from
$header.="X-Priority: 3 (Normal)\r\n";
$header.="Message-ID: <172562218.".date("YmjHis")."@domain.ru>\r\n"; //, где domain.ru - mailing domain
$header.="To: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('test_2')))."?= <asd@domain.ru>\r\n"; //, где test_2 - the recipient name, asd@domain.ru - the recipient e-mail
$header.="Subject: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('check')))."?=\r\n"; //, where "check" is the theme of the letter
$header.="MIME-Version: 1.0\r\n";
$header.="Content-Type: text/plain; charset=windows-1251\r\n";
$header.="Content-Transfer-Encoding: 8bit\r\n";
?>

If the letters are displayed in the wrong encoding you have to specify the correct encoding is Windows-1251.

We have created the variable with the titles, let’s create a variable with the text of the letter.

$text="hello, check out.";

Open the connection with SMTP server.

$smtp_conn = fsockopen("smtp.mail.ru", 25,$errno, $errstr, 10);

When the connection is opened read the server answer in the variable $data

$data = get_data($smtp_conn);

To send e-mail run the following commands:

<?
fputs($smtp_conn,"EHLO vasya\r\n"); //, where
greet the server, introduce yourself. Type your computer name after EHLO (it can be random)
<$data = get_data($smtp_conn);

fputs($smtp_conn,”AUTH LOGIN\r\n”); //, call authorization command
$data = get_data($smtp_conn);

fputs($smtp_conn,base64_encode(“login”).”\r\n”); // enter your login
$data = get_data($smtp_conn);

fputs($smtp_conn,base64_encode(“password”).”\r\n”); //, enter your password
$data = get_data($smtp_conn);

// count the number of characters in email (with all titles) to convey, what is the letter size
$size_msg=strlen($header.”\r\n”.$text);

fputs($smtp_conn,”MAIL FROM:<login@domain.ru> SIZE=”.$size_msg.”\r\n”); //, где login@domain.ru – the e-mail of the sender
$data = get_data($smtp_conn);

fputs($smtp_conn,”RCPT TO:<asd@domain.ru>\r\n”); //, where asd@domain.ru – e-mail of the recipient.
$data = get_data($smtp_conn);

fputs($smtp_conn,”DATA\r\n”);
$data = get_data($smtp_conn);

fputs($smtp_conn,$header.”\r\n”.$text.”\r\n.\r\n”);
$data = get_data($smtp_conn);

fputs($smtp_conn,”QUIT\r\n”);
$data = get_data($smtp_conn);
?>


After the input of every command you need to know if the server accepted the command or not. You must receive the first three characters of the line:

$code = substr($data,0,3);
echo ($code);

The full php script to send email:

<?
function get_data($smtp_conn)
{
$data="";
while($str = fgets($smtp_conn,515))
{
$data .= $str;
if(substr($str,3,1) == " ") { break; }
}
return $data;
}

$header="Date: ".date("D, j M Y G:i:s")." +0700\r\n";
$header.="From: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('test_1')))."?= <login@domain.ru>\r\n"; //, где Windows-1251 - encoding, test_1 - the sender name, login@domain.ru - the e-mail to send from

$header.="X-Mailer: The Bat! (v3.99.3) Professional\r\n";
$header.="Reply-To: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('test_1')))."?= <login@domain.ru>\r\n"; //, где Windows-1251 - encoding, test_1 - the sender name,
login@domain.ru - the e-mail to send from
$header.="X-Priority: 3 (Normal)\r\n";
$header.="Message-ID: <172562218.".date("YmjHis")."@domain.ru>\r\n"; //, где domain.ru - the mailing domain
$header.="To: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('test_2')))."?= <asd@domain.ru>\r\n"; //, где test_2 - the recipient name, asd@domain.ru - the sender e-mail
$header.="Subject: =?windows-1251?Q?".str_replace("+","_",str_replace("%","=",urlencode('check')))."?=\r\n"; //, where "check" is the theme of the letter
$header.="MIME-Version: 1.0\r\n";
$header.="Content-Type: text/plain; charset=windows-1251\r\n";
$header.="Content-Transfer-Encoding: 8bit\r\n";

$text="hello, check out.";

$smtp_conn = fsockopen("smtp.mail.ru", 25,$errno, $errstr, 10);

$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,"EHLO vasya\r\n"); //, где greet the server, introduce yourself. Type your computer name after EHLO (it can be random)
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,"AUTH LOGIN\r\n"); //, call authorization command
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,base64_encode("login")."\r\n"); //, enter your login
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,base64_encode("password")."\r\n"); //, enter your password
$data = get_data($smtp_conn);


//$code = substr($data,0,3);
//echo ($code);
// count the number of characters in email (with all titles) to convey, what is the letter size
$size_msg=strlen($header."\r\n".$text);

fputs($smtp_conn,”MAIL FROM:<login@domain.ru> SIZE=”.$size_msg.”\r\n”); //, гwhere login@domain.ru – the sender e-mail
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,"RCPT TO:<asd@domain.ru>\r\n"); //, where asd@domain.ru - the recipient e-mail.
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,"DATA\r\n");
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,$header."\r\n".$text."\r\n.\r\n");
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);

fputs($smtp_conn,"QUIT\r\n");
$data = get_data($smtp_conn);

//$code = substr($data,0,3);
//echo ($code);
?>