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
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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.