...

View Full Version : php code - Sending mail with external SMTP server



galford
09-02-2011, 06:48 AM
Hello i'm working on a project and i've reach a dead end. What i'm trying to do with the code is this:

1. Connecting to an external smtp server;
2. Authenticating with login and pass using AUTH LOGIN using an external hostnames file (line-by-line), and external user and password file.
3. Send a email to a predefined email address on file using an external file with the email body.

My problem is that I dont receive any email, what i'm guessing is that the auth login code is broken. Also I want to make a single user and password file using this format user : password. Also I want to make the code do this: to get the first smtp hostname and to login with the users:password in the external file provided. If unsuccessful login to try the next line in the users : password file. When finished with the login and password to jump to the next hostname in the hostname file.

Here's my work so far:



<?php
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{

function loadini($path) {
$fp = fopen($path, "r");
fclose($fp);
}

$message = loadini("message");
$to = "broken@code.com";
$from = "test@test.com";
$namefrom = "test";
$subject = "test also";
$nameto = "notme";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);

?>
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = loadini("logfile");;
$port = "25";
$timeout = "30";
$username = loadini("users");
$password = loadini("password");
$localhost = "localhost";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}

//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";

//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";

//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";

//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";

//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";

//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";

//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";

// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>

djm0219
09-02-2011, 07:00 AM
May I suggest obtaining and using phpMailer (http://phpmailer.worxware.com/) which will make your life a LOT easier.

galford
09-02-2011, 07:05 AM
May I suggest obtaining and using phpMailer (http://phpmailer.worxware.com/) which will make your life a LOT easier.

Since its a school project i need to finish that code. Can u give me some tips since I'm quite new to this?

djm0219
09-02-2011, 07:23 AM
Well some debugging is certainly in order. You appear to be stashing responses in an array named $logArray but you don't display it so you can't tell what you are getting back from the server. Use print_r at various points to see what responses you are getting which should give you a clue to what is going wrong where.

galford
09-02-2011, 07:25 AM
Well some debugging is certainly in order. You appear to be stashing responses in an array named $logArray but you don't display it so you can't tell what you are getting back from the server. Use print_r at various points to see what responses you are getting which should give you a clue to what is going wrong where.

Like I said I'm quite noobish... Can you please modify the code for my needs and repaste it here? I will be quite grateful.

djm0219
09-02-2011, 07:50 AM
Errr, no but I will give you a clue.


$logArray['connection'] = "Connected: $smtpResponse";

You're capturing the response after every interaction but you aren't testing to see whether or not the interaction worked. Start by displaying the responses you are getting so you can see what is happening. Something like this:



$logArray['connection'] = "Connected: $smtpResponse";
echo 'Response from connection attempt was: ' . $smtpResponse;


Add that echo with appropriate changes to where you are any time you are getting a response back.


$logArray['heloresponse'] = "$smtpResponse";
echo 'Response from HELO was: ' . $smtpResponse;

galford
09-02-2011, 07:57 AM
Ok i'll add those lines and see what went wrong. I'll come back.

galford
09-02-2011, 08:26 AM
I added those lines and yet to be amazed no echos, hope I made it right. The code is:



<?php
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{


function loadini($path) {
$fp = fopen($path, "r");
$fpcontents = fread($fp, filesize($path));
fclose($fp);
return $fpcontents;
}

$message = loadini("message");
$to = "broken@code.com";
$from = "test@test.com";
$namefrom = "test";
$subject = "test also";
$nameto = "notme";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);


//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = loadini("logfile");;
$port = "25";
$timeout = "30";
$username = loadini("users");
$password = loadini("password");
$localhost = "localhost";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";

}
echo 'Response from connection attempt was: ' . $smtpResponse;
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
echo 'Response from HELO was: ' . $smtpResponse;

//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
echo 'Response from AUTH LOGIN was: ' . $smtpResponse;


//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
echo 'Response from USERNAME was: ' . $smtpResponse;

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
echo 'Response from PASSWORD was: ' . $smtpResponse;

//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
echo 'Response from EMAIL FROM was: ' . $smtpResponse;

//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
echo 'Response from EMAIL TO was: ' . $smtpResponse;

//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
echo 'Response from DATA was: ' . $smtpResponse;

//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
echo 'Response from EMAIL RESPONSE was: ' . $smtpResponse;

// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
echo 'Response from QUIT was: ' . $smtpResponse;
}
?>

Any suggestions? Bare with me a little longer.

djm0219
09-02-2011, 08:37 AM
Sounds like your initial connection may not be working. Trying echoing the response from the connection attempt itself.



$smtpResponse = fgets($smtpConnect, 515);
echo 'Connection response was: ' . $smtpResponse;


I also notice that you appear to be recursively calling authSendEmail which may not be what you really mean to be doing (look at the last line of the snippet of your code that I pasted below). At the very least I would expect an error from PHP complaining about loadini being declared more than once. Do you have error reporting enabled?



function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{


function loadini($path) {
$fp = fopen($path, "r");
$fpcontents = fread($fp, filesize($path));
fclose($fp);
return $fpcontents;
}

$message = loadini("message");
$to = "broken@code.com";
$from = "test@test.com";
$namefrom = "test";
$subject = "test also";
$nameto = "notme";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);


It would also help if you would use PHP tags around your code instead of CODE tags.

jhone0102
09-02-2011, 08:48 AM
Hi,
welcome to this Forum Site here u can find lots of thing in no minutes.i think for your doubt you should ask with your friends & i hope you will get your answer,thanks.....:)

Top Ten Classified Websites (http://www.alladsclassified.com)

galford
09-02-2011, 08:50 AM
I have error reporting on. Added those lines and still no echos.

Here's the code:



<?php
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{

function loadini($path) {
$fp = fopen($path, "r");
$fpcontents = fread($fp, filesize($path));
fclose($fp);
return $fpcontents;
}
$message = loadini("message");
$to = "broken@code.com";
$from = "test@test.com";
$namefrom = "test";
$subject = "test also";
$nameto = "notme";

authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);



//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = loadini("logfile");;
$port = "25";
$timeout = "30";
$username = loadini("users");
$password = loadini("password");
$localhost = "localhost";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
echo 'Connection response was: ' . $smtpResponse;

if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";

}
echo 'Response from connection attempt was: ' . $smtpResponse;

//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
echo 'Response from HELO was: ' . $smtpResponse;

//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
echo 'Response from AUTH LOGIN was: ' . $smtpResponse;


//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
echo 'Response from USERNAME was: ' . $smtpResponse;

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
echo 'Response from PASSWORD was: ' . $smtpResponse;

//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
echo 'Response from EMAIL FROM was: ' . $smtpResponse;

//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
echo 'Response from EMAIL TO was: ' . $smtpResponse;

//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
echo 'Response from DATA was: ' . $smtpResponse;

//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
echo 'Response from EMAIL RESPONSE was: ' . $smtpResponse;

// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
echo 'Response from QUIT was: ' . $smtpResponse;
}
?>

djm0219
09-02-2011, 09:02 AM
What is calling the authSendEmail function? If you are not getting any output at all it would seem that the function is never being called.

galford
09-02-2011, 09:09 AM
I erased that line.

Connection response was: 220 **************************************************************************************************** ******************
Response from connection attempt was: 220 **************************************************************************************************** ******************
Response from HELO was: 250 localhost Hello [localhost]
Response from AUTH LOGIN was: 503 5.5.2 Send hello first.
Response from USERNAME was: 500 5.3.3 Unrecognized command
Response from PASSWORD was: 500 5.3.3 Unrecognized command

it seems like after send HELO command it stucks. But the smtp server receives the HELO command.

djm0219
09-02-2011, 09:24 AM
Now you're getting somewhere. The response from HELO appear to be correct though the SMTP server you are trying to talk to may not be happy with localhost or it may be expecting EHLO instead of HELO.

The AUTH command expects the first parameter to be the type of authentication to be used not the word LOGIN

galford
09-02-2011, 09:56 AM
All looks fine now except the mailing procedure.

Connection response was: 220 **************************************************************************************************** ******************
Response from connection attempt was: 220 **************************************************************************************************** ******************
Response from HELO was: 250-localhost [localhost]
Response from AUTH LOGIN was: 250-XXXA
Response from USERNAME was: 250-SIZE
Response from PASSWORD was: 250-ETRN
Response from EMAIL FROM was: 250-PIPELINING
Response from EMAIL TO was: 250-DSN
Response from DATA was: 250-ENHANCEDSTATUSCODES
Response from EMAIL RESPONSE was: 250-8bitmime
Response from QUIT was: 250-BINARYMIME

I'm guessing its not implemented. That function that I erased but needs to be reworked I guess since i'm not receiving the email.

djm0219
09-02-2011, 10:24 AM
Instead of echoing a single newline on your first DATA command start echoing your headers and other data instead of building them up and sending them all at once. I also noticed that in your last command you are separating the To, From, Subject and body with just \n which isn't going to work.



//The Email
fputs($smtpConnect, 'DATA MIME-Version: 1.0' . $newLine);
fputs($smtpConnect, 'Content-type: text/html; charset=iso-8859-1' . $newLine;
fputs($smtpConnect, 'To: ' . $nameto . '<' . $to . '>' . $newLine;
fputs($smtpConnect, 'From: ' . $namefrom . '<' . $from . '>' . $newLine;
fputs($smtpConnect, 'Subject: ' . $subject . $newLine . $newLine;
fputs($smtpConnect, $message . $newLine;
fputs($smtpConnect, '.' . $newLine;

galford
09-02-2011, 10:39 AM
Hmmm I modified it but I still missing something since I'm not receiving any mail.

Output is:



Connection response was: 220 **************************************************************************************************** ******************
Response from connection attempt was: 220 **************************************************************************************************** ******************
Response from HELO was: localhost Hello [localhost]
Response from AUTH LOGIN was: 250-XXXA
Response from USERNAME was: 250-SIZE
Response from PASSWORD was: 250-ETRN
Response from THE EMAIL was: 250-PIPELINING
Response from QUIT was: 250-DSN



<?php
<?php
//new function

function loadini($path) {
$fp = fopen($path, "r");
$fpcontents = fread($fp, filesize($path));
fclose($fp);
return $fpcontents;
}


$to = "test@test.com";
$nameto = "notme";
$from = "test";
$namefrom = "Who From";
$subject = "Hello World Again!";
$message = loadini("message");


//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = loadini("logfile");;
$port = "25";
$timeout = "30";
$username = loadini("users");
$password = loadini("password");
$localhost = "user";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
echo 'Connection response was: ' . $smtpResponse;

if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";

}
echo 'Response from connection attempt was: ' . $smtpResponse;

//Say Hello to SMTP
fputs($smtpConnect, "EHLO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
echo 'Response from HELO was: ' . $smtpResponse;

//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
echo 'Response from AUTH LOGIN was: ' . $smtpResponse;


//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
echo 'Response from USERNAME was: ' . $smtpResponse;

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
echo 'Response from PASSWORD was: ' . $smtpResponse;

//The Email
fputs($smtpConnect, 'DATA MIME-Version: 1.0' . $newLine);
fputs($smtpConnect, 'Content-type: text/html; charset=iso-8859-1' . $newLine);
fputs($smtpConnect, 'To: ' . $nameto . '<' . $to . '>' . $newLine);
fputs($smtpConnect, 'From: ' . $namefrom . '<' . $from . '>' . $newLine);
fputs($smtpConnect, 'Subject: ' . $subject . $newLine . $newLine);
fputs($smtpConnect, $message . $newLine);
fputs($smtpConnect, '.' . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
echo 'Response from THE EMAIL was: ' . $smtpResponse;


// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
echo 'Response from QUIT was: ' . $smtpResponse;
?>

djm0219
09-02-2011, 11:13 AM
There is nothing there indicating an error. I'd double check your To address and perhaps wait a bit. Mail does not always get delivered instantly :) You can always hard code a To address to make sure it is correct and you may also try it without a name ... ie:


fputs($smtpConnect, 'To: somename@address.com' . $newLine);

galford
09-02-2011, 11:39 AM
Hmm still aint receiving any email. I think there is a problem with geting the users and the password from the external files since i modified the password file and wrote a wrong password and still getting the same output. Cant it be made so the procedure will use only one file in format <user>:<password> since having both user and password file eventualy will mix users with passwords?

Here is the initial procedure:



function loadini($path) {
$fp = fopen($path, "r");
$fpcontents = fread($fp, filesize($path));
fclose($fp);
return $fpcontents;
}
$username = loadini("users");
$password = loadini("password");


Modified headers:




//The Email
fputs($smtpConnect, 'DATA MIME-Version: 1.0' . $newLine);
fputs($smtpConnect, 'Content-type: text/html; charset=iso-8859-1' . $newLine);
fputs($smtpConnect, 'To: email@mine.com' . $newLine);
fputs($smtpConnect, 'From: someone@else.com' . $newLine);
fputs($smtpConnect, 'Subject: ' . $subject . $newLine . $newLine);
fputs($smtpConnect, $message . $newLine);
fputs($smtpConnect, '.' . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
echo 'Response from THE EMAIL was: ' . $smtpResponse;

djm0219
09-02-2011, 11:44 AM
You may alter it to do anything you'd like it to do. Based on the responses you are getting it does not look like a user/password problem.

galford
09-02-2011, 11:51 AM
Hmm well then how can I make a loop... if there is a wrong password to move in the next line of the external password file. Since most users are keep changing their passwords and i have to add several users/passwords. But first things first, what is going wrong so far?

djm0219
09-02-2011, 12:15 PM
Hmm well then how can I make a loop... if there is a wrong password to move in the next line of the external password file. Since most users are keep changing their passwords and i have to add several users/passwords. But first things first, what is going wrong so far?

As I said, there is nothing in what you have posted so far that indicates there are any errors in the sending of the data. I would probably look at the sendmail logs of the SMTP host you are trying to use to see if there are any errors there. You might also be getting errors sent back to the From address.

galford
09-02-2011, 12:44 PM
Just verified manually the SMTP and works, received the mail instantly. It seems like something else goes wrong. I'm guess the headers. Can't you test the code and see what is going wrong?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum