Jump to content

smtp authantication


munno

Recommended Posts

Hi all,

 

I am a newbie to PHP world and just began to explore into it. I have created one website for our community (not for profit) using free software. There is a "forgot password" function which emails user details. Unfortunately, it only works with inbuilt mail function of PHP. My host does not support this and they need SMTP authentication and support PEAR mail extension or PHPMailer. Below is the code and I need to change it so that it works with PEAR extension or PHPMailer with SMTP authentication.

 

Can someone please help me to fix code.

 

<?php
require('connection.php');
require_once('users.php');
require_once('functions.php');
$err_string = "";
@session_start();
if (isset($_POST["act"])) {
    $emailrequest = qsrequest("email");
    $header_body   = "";
    $header_body .= " Header for body E-mail\n";
    $footer_body   = "";
    $footer_body .= " Footer for body E-mail\n";
    $msg_success   = "";
    $msg_success .= " <center>Successfully sent E-mail.</center>\n";
    $msg_success   .= "<br><br><a href=\"javascript:self.history.back();\">Go Back </a>";
    $str_emailfrom = "me@me.org";
    $str_subject = "Your Password Reminder Request.";
    $str_emailcc = "";
    $str_emailbcc = "";
    $str_replyto = "you@you.org";
    $email_priority = "3"; // 1 = High , 3 = Normal, 5 = Low
    $userlevel = 0;
    $ifound = 0;
    $emailrequest = qsrequest("email");
    $sql = "";
$sql .= "  SELECT `User_Name` , `User_Password` , `User_Level` , `User_Email` FROM `table`\n";
    $sql .= " WHERE ";
    $sql .= "User_Email = '" . $emailrequest . "'";
    if(!$result = @mysql_query($sql)){
        $err_string .= "<strong>Error:</strong> while connecting to database<br>" . mysql_error();
    }else{
        $num_rows = mysql_num_rows($result);
        $row = mysql_fetch_array($result);
        $ifound = $num_rows;
    }
    if ($ifound > 0) {
        $str_body = "";
        $str_body = $str_body . $header_body . "\n\n" . "User name : " . $row[0] . "\n" . "Password : " . $row[1]  . "\n\n" . $footer_body;
        $str_emailto = $emailrequest;
        $header  = "";
        $header  = "From:".$str_emailfrom."\r\n";
        $header .= "Reply-to:".$str_replyto."\r\n";
        $header .= "Cc:".$str_emailcc ."\r\n";
        $header .= "Bcc:".$str_emailbcc ."\r\n";
        $header .= "X-Priority : ".$email_priority."\n";
        $success = @mail($str_emailto, $str_subject, $str_body, $header);
        if (!$success) {
            print "<br><br><font color=\"FF0000\"><center><center>Cannot send your E-mail.</center></center></font>";
        } else { //' Successfully send email
            print $msg_success;
        }
    } else {
            print "<br><br><font color=\"FF0000\"><center>Sorry, E-mail not found!</center></font>";
    }
}
?>

 

I was told to use this example (http://www.mdwebhosting.com.au/support/wiki/index.php/HOWTO_SMTP_PHP). I tried just replacing header, body etc. tags but then they are also referenced into the sql component at the bottom of code and that is where I got lost. I could replace them too but have no idea of integrity of code.

 

Please help me and/or point me into right direction.

 

Regards,

Link to comment
Share on other sites

Hi again,

 

I gave it a try and below is the edited version. Can you please verify that below is proper code and let me know if something is wrong. Please help me guys.

 

<?php
require('connection.php');
require_once('users.php');
require_once('functions.php');
require_once "Mail.php";
$err_string = "";
@session_start();
if (isset($_POST["act"])) {
    $emailrequest = qsrequest("email");
    $header_body   = "";
    $header_body .= " Header for body E-mail\n";
    $footer_body   = "";
    $footer_body .= " Footer for body E-mail\n";
    $msg_success   = "";
    $msg_success .= " <center>Successfully sent E-mail.</center>\n";
    $msg_success   .= "<br><br><a href=\"javascript:self.history.back();\">Go Back </a>";
    $from = "sender@example.com";
    $to = "recipient@example.com";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";
    $host = "localhost";
    $username = "smtp_username";
    $password = "smtp_password"; 
     
    $headers = array ('From' => $from,
       'To' => $to,
       'Subject' => $subject);
    $smtp = Mail::factory('smtp',
       array ('host' => $host,
         'auth' => true,
         'username' => $username,
     'password' => $password));
    $emailrequest = qsrequest("email");
    
    $sql = "";
$sql .= "  SELECT `User_Name` , `User_Password` , `User_Level` , `User_Email` FROM `table`\n";
    $sql .= " WHERE ";
    $sql .= "User_Email = '" . $emailrequest . "'";
    if(!$result = @mysql_query($sql)){
        $err_string .= "<strong>Error:</strong> while connecting to database<br>" . mysql_error();
    }else{
        $num_rows = mysql_num_rows($result);
        $row = mysql_fetch_array($result);
        $ifound = $num_rows;
    }
    if ($ifound > 0) {
        $str_body = "";
        $str_body = $str_body . $header_body . "\n\n" . "User name : " . $row[0] . "\n" . "Password : " . $row[1]  . "\n\n" . $footer_body;
        $str_emailto = $emailrequest;
        $header  = "";
        $header  = "From:".$from."\r\n";
        $success = @mail($to, $subject, $body, $header);
        if (!$success) {
            print "<br><br><font color=\"FF0000\"><center><center>Cannot send your E-mail.</center></center></font>";
        } else { //' Successfully send email
            print $msg_success;
        }
    } else {
            print "<br><br><font color=\"FF0000\"><center>Sorry, E-mail not found!</center></font>";
    }
}
?>

 

 

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.