FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Mail() function and SMTP





hfourxzeror
Hello, im new to this forum. I'm working on getting a hosting account through Frihost.

Let me begin by saying that is is a basic mail() function PHP code that also has SMTP built into it.

The PHP Code: "Spoof.php"
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP + SMTP</title>
</head>
<body>
<?php
//new function

$to = $_POST['to'];
$nameto =  $_POST['toname'];
$from = $_POST['from'];
$namefrom = $_POST['fromname'];
$subject = $_POST['subject'];
$message = $_POST['message'];
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
?>


<?php
/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */ 

//Authenticate Send - 21st March 2005
//This will send an email using auth smtp and output a log array
//logArray - connection, 

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
    //SMTP + SERVER DETAILS
    /* * * * CONFIGURATION START * * * */
    $smtpServer = "ENTER SMTP SERVER HERE!";
    $port = "25";
    $timeout = "30";
    $username = "ENTER SMTP USERNAME HERE!";
    $password = "ENTER SMTP SERVER PASS HERE!";
    $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";
    }

    //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";

    //Say Hello to SMTP
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['heloresponse'] = "$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";     
}
?>

</body>
</html>


and the HTML code, "index.html"

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="spoof.php" method="post">
<br />
<table width="488" border="1">
  <tr>
    <td width="92">From:</td>
    <td width="380"><input name="From" type="text" size="50" /></td>
  </tr>
  <tr>
    <td>From Name: </td>
    <td><input name="fromname" type="text" size="50" /></td>
  </tr>
  <tr>
    <td>To:</td>
    <td><input name="to" type="text" size="50" /></td>
  </tr>
  <tr>
    <td>To Name: </td>
    <td><input name="toname" type="text" size="50" /></td>
  </tr>
  <tr>
    <td>Subject:</td>
    <td><input name="subject" type="text" size="50" /></td>
  </tr>
  <tr>
    <td>Message</td>
    <td><textarea name="message" cols="47" rows="10"></textarea></td>
  </tr>
  <tr>
    <td>Button --&gt; </td>
    <td><input name="send" type="button" value="CLICK THIS BUTTON TO SEND THIS MESSAGE VIA SMTP!!" /></td>
  </tr>
</table>
<br />
<br />
<br />
<br />
<br />
</form>

</body>
</html>


Hope this was useful!

EZ Very Happy [/code]
X3 Talk
I tend to find that I just need to use the mail() function. You have to configure an SMTP through the PHP Config file, but it is quite easy. You could also get it to loop through all the vars in the $_POST array if you were making a long question setup and you wanted the results emailed to you.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.