Jump to content

sending email form help.


herghost

Recommended Posts

Hi all,

 

I have this:

<?php
include('../../config/connect.php');
$id = $_GET['id'];
$companyname="SELECT name FROM company_details WHERE id='1'";
$result = mysql_query($companyname);
if (!$result)
{
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_assoc($result);
mysql_free_result($result);

$customerdetails="SELECT * FROM customers WHERE id='$id'";
$result0 = mysql_query($customerdetails);
if (!$result0)
{
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row0 = mysql_fetch_assoc($result0);
mysql_free_result($result0);

$smtpdetails="SELECT * FROM smtp";
$result1 = mysql_query($smtpdetails);
if (!$result1)
{
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row1 = mysql_fetch_assoc($result1);
mysql_free_result($result1);


//Details Needed
$companyname = $row['name'];
$companyemail = $row['email'];

$customername = $row0['forname'];
$customeremail = $row0['email'];

$smtpserver = $row1['server'];
$smtpusername = $row1['username'];
$smtppassword = $row1['password'];
$smtpport = $row1['port'];
//


$to = $customeremail;
$nameto = $customername;
$from = $companyemail;
$namefrom = $companyname;
$subject = " Welcome to " . $companyname . "";
$message = "Message to go here";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
?>


<?php


function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = $smtpserver;
$port = $smtpport;
$timeout = "30";
$username = $smtpusername;
$password = $smtppassword;
$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"; 
}

$_SESSION['welcome'] = 'welcome';
?>

 

Which is simply meant to send an welcome email to a user, however what is the solution to this error?

Warning: fsockopen() [function.fsockopen]: unable to connect to :0 (Failed to parse address "")

 

I know it concerns this block of code:

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = $smtpserver;
$port = $smtpport;
$timeout = "30";
$username = $smtpusername;
$password = $smtppassword;
$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";
}

 

Can I not use the variables to get this info as I have?

 

Thanks

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/196446-sending-email-form-help/
Share on other sites

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = $smtpserver;
$port = $smtpport;

 

Where is $smtpserver and $smtpport defined? They aren't, therefor fopen (fputs) has nothing to connect to.

Make the below change to your smtp query section to make sure you are pulling back data from the database.  Because it appears fsockopen isnt getting passed an address.  Your variables should be fine outside of the function but its better practice to pass them in as arguments or move the whole below section into the mail function.

 

$smtpdetails="SELECT * FROM smtp";
$result1 = mysql_query($smtpdetails);
if (!$result1)
{
    echo 'Could not run query: ' . mysql_error();
    exit;
}

if (mysql_num_rows($result1) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

$row1 = mysql_fetch_assoc($result1);
mysql_free_result($result1);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.