munno Posted November 8, 2007 Share Posted November 8, 2007 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 = "[email protected]"; $str_subject = "Your Password Reminder Request."; $str_emailcc = ""; $str_emailbcc = ""; $str_replyto = "[email protected]"; $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 https://forums.phpfreaks.com/topic/76495-smtp-authantication/ Share on other sites More sharing options...
munno Posted November 9, 2007 Author Share Posted November 9, 2007 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 = "[email protected]"; $to = "[email protected]"; $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 https://forums.phpfreaks.com/topic/76495-smtp-authantication/#findComment-388212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.