[PHP] Email Function

Results 1 to 8 of 8
  1. #1
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    [PHP] Email Function

    PHP Code:
        <!-- Reset Password Start -->
        <div id="tbl_head">
         <div class="tbl_title_pos">
          <?php echo $title['22']; ?>
         </div>
        </div>
        <div id="tbl_body">
         <div class="tbl_pos">
          <form action="index.php?act=forgottenpassword" method="post">
          <table width="70%" align="center" style="font-weight:bold;">
           <tr><td><?php echo $fopass['2'].' : ';?></td> <td><input type="text" name="fuserid" class="tbl_colbor" /></td></tr>
           <tr><td><?php echo $fopass['3'].' : ';?></td> <td><input type="text" name="femail" class="tbl_colbor" /></td></tr>
           <tr><td></td><td><input type="submit" name="sendpassword" value="<?php echo $fopass['1']; ?>" />
          </table>
          </form>
          <div class="forgpaser">
         <?php
    // Using the ini_set()
    ini_set("SMTP""mail.domain.com");
    ini_set("sendmail_from""user@domain.com");
    ini_set("smtp_port""25");
          if(isset(
    $_POST['sendpassword'])){
           
    $userid anti_injection($_POST['fuserid']);
           
    $email anti_injection($_POST['femail']);
            if(!
    $userid&&!$email){
              echo 
    $fopass['4'];
            }else{
              
    $fsql mssql_query("SELECT UserID FROM Account WHERE UserID='$userid'");
              if(
    mssql_num_rows($fsql)<>0){
              
    $fcusid mssql_fetch_assoc($fsql);
               
    $fsql2 mssql_query("SELECT Email FROM Account WHERE UserID='".$fcusid['UserID']."'");
               if(
    mssql_num_rows($fsql2)<>0){
               
    $fcemail mssql_fetch_assoc($fsql2);
                
    $fsql3 mssql_query("SELECT Email FROM Account WHERE Email='".$fcemail['Email']."'");
                if(
    mssql_num_rows($fsql3)<>0){
                
    $coremail mssql_fetch_assoc($fsql3);
                  if(
    $email == $coremail['Email']){
                   
    $fsql4 "SELECT * FROM Login WHERE UserID='".$fcusid['UserID']."'";
                   
    $fcnquery mssql_query($fsql4);
                   
    $password mssql_fetch_assoc($fcnquery);
                   
    $pass $password['Password'];
                   
                   
    $text 'Your '.$gunz_name.' Password : '.$pass;
                    
    mail($coremail['Email'],'Your '.$gunz_name.' Password',$text,'From: '.$gunz_name.' <email@domain.com>');
                    echo 
    $fopass['7'];
                    
                  }else{
                    echo 
    $fopass['6'];
                  }
                }
               }
              }else{
                echo 
    $fopass['5'];
              }
            }
          }
         
    ?>
         </div>
         </div>
        </div>
        <div id="tbl_footer"></div><br /><br />
        <!-- Reset Password End -->
    I have the script running to where it says it successfully sent the E-mail, but I dont get any email from this.
    Anything I missed?


  2. #2
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: [PHP] Email Function

    You must have a mail server running.
    Refer to the mail part in php.ini.

    If you have a mail server, use this.

  3. #3
    Die() Secured is offline
    MemberRank
    Sep 2011 Join Date
    /home/SDev/Location
    555Posts

    Re: [PHP] Email Function

    Email Function with HTML Support (Not Tested)
    PHP Code:
    <?php

    function sendEmail($to$from$subject$message$cc NULL$bcc NULL)
    {

        
    $headers "MIME-Version: 1.0rn"
        
    $headers .= "Content-type: text/html; charset=iso-8859-1rn"
        
    $headers .= "From: ".$from."\r\n"
        
        if(
    $cc != NULL){

            
    $headers .= "Cc: [email]".$cc."[/email]"
        }
        
        if(
    $bcc != NULL){

            
    $headers .= "Bcc: [email]".$bcc."[/email]"
        }
         
        if (
    mail($to$subject$message$headers){

            return 
    true;
            
        } else {
        
            return 
    false;
        }
    }
    ?>
    Usage:
    PHP Code:
    <?php
    $message 
    "<html>
        <body bgcolor=\"#DCEEFC\"> 
            <center> 
                <b>Email Stuff</b>
            </center> 
        </body> 
    </html>"
    ;

    //args 5 and 6 can either be NULL or just Just left empty. Ex: sendEmail("user@user.com", "no-reply@site.com", "Nice Subject", $message);
    if(sendEmail("t@to.com""f@from.com""Subject"$message"cc@cc.com"NULL) == TRUE)
    {
        echo 
    "email was sent";

    } else {

        echo 
    "error sending email";
    }
    ?>
    Credits Me. I dont really like the SMTP one.
    Last edited by Secured; 09-10-12 at 08:48 AM.

  4. #4
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: [PHP] Email Function

    Quote Originally Posted by SuperWaffle View Post
    You must have a mail server running.
    Refer to the mail part in php.ini.

    If you have a mail server, use this.
    We have a mail server.
    Quote Originally Posted by Secured View Post
    Email Function with HTML Support (Not Tested)
    PHP Code:
    <?php

    function sendEmail($to$from$subject$message$cc NULL$bcc NULL)
    {

        
    $headers "MIME-Version: 1.0rn"
        
    $headers .= "Content-type: text/html; charset=iso-8859-1rn"
        
    $headers .= "From: ".$from."\r\n"
        
        if(
    $cc != NULL){

            
    $headers .= "Cc: [email]".$cc."[/email]"
        }
        
        if(
    $bcc != NULL){

            
    $headers .= "Bcc: [email]".$bcc."[/email]"
        }
         
        if (
    mail($to$subject$message$headers){

            return 
    true;
            
        } else {
        
            return 
    false;
        }
    }
    ?>
    Usage:
    PHP Code:
    <?php
    $message 
    "<html>
        <body bgcolor=\"#DCEEFC\"> 
            <center> 
                <b>Email Stuff</b>
            </center> 
        </body> 
    </html>"
    ;

    //args 5 and 6 can either be NULL or just Just left empty. Ex:  sendEmail("user@user.com", "no-reply@site.com", "Nice Subject",  $message);
    if(sendEmail("t@to.com""f@from.com""Subject"$message"cc@cc.com"NULL) == TRUE)
    {
        echo 
    "email was sent";

    } else {

        echo 
    "error sending email";
    }
    ?>
    Credits Me. I dont really like the SMTP one.
    I don't quite understand how it works.

  5. #5
    Die() Secured is offline
    MemberRank
    Sep 2011 Join Date
    /home/SDev/Location
    555Posts

    Re: [PHP] Email Function

    Quote Originally Posted by wesman2232 View Post
    We have a mail server.

    I don't quite understand how it works.
    Use's PHPs mail() Function and sends with an email with to $to from $from including the subject $subject and the message $message, you can also add CC and BCC Im sure you can add more than 1 by if array then loop it. I didn't really read threw the script you posted, but this one aswell uses the default SMTP Server which is set in the PHP ini. If the server is hosted threw a hoster it should work without an issue, if its linux it should work with its default sendmail program. But windows :x I guess what I did was register my domain up with gmail threw google apps, then I set the smtp server to smtp.gmail.com or w/e it is and sent emails threw the created emails. But I've done this threw localhost with xampp & winampp int he past.

    Lets see the smtp section of your php ini, and your smtp server information.

    Note: the only difference between mine and the function in your script is that mine is using MIME type headers which would allow to create a much fancier looking email, with html and images.

  6. #6
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: [PHP] Email Function

    I don't have access to the VPN that the script is hosted on. I just have the exact same files on my localhost, then send updates to the hoster. Which is why im using the ini_set function.
    I'm just confused on the part where it says "t@to.com", "f@from.com"
    'From', ok, but wouldnt the 'to' need a email grabbed from the DB and then set there so any of the users could reset their pass?
    (This is part of the Forgot Password function in the web)

    Is the mail server required to be on that VPN or can it still be defined with the ini_set function?

  7. #7
    Die() Secured is offline
    MemberRank
    Sep 2011 Join Date
    /home/SDev/Location
    555Posts

    Re: [PHP] Email Function

    Quote Originally Posted by wesman2232 View Post
    I don't have access to the VPN that the script is hosted on. I just have the exact same files on my localhost, then send updates to the hoster. Which is why im using the ini_set function.
    I'm just confused on the part where it says "t@to.com", "f@from.com"
    'From', ok, but wouldnt the 'to' need a email grabbed from the DB and then set there so any of the users could reset their pass?
    (This is part of the Forgot Password function in the web)

    Is the mail server required to be on that VPN or can it still be defined with the ini_set function?
    I could just write a script for you? Wouldnt be hard, lemme wip something up. Really quick. I don't have access to the a db so i cant really right the queries off memory so someone else your you will have to do it.

    Edit:
    Like I said I didnt write any queries for this, so i left them blank you can edit the queries, i explained what each one needs todo and an example of it might be. Like I said I dont have access to a db so im unsure. This may require more or less queries than I put but this should be correct. I'm sure someone here will correct me if im wrong.

    Note: I wrote this as a function so you can just use the example below it.
    PHP Code:
    <?php
    //maybe not the best anti-sql but ya, anyone feel free to make it better.
    function anti_injection($str) {
       
    $str preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"),"",$sql);
       
    $str trim($sql);
       
    $str strip_tags($sql);
       
    $str addslashes($sql);
       return 
    $str;
    }

    function 
    sendEmail($to$from$subject$message)
    {

        
    $headers "MIME-Version: 1.0rn"
        
    $headers .= "Content-type: text/html; charset=iso-8859-1rn"
        
    $headers .= "From: ".$from."\r\n"
         
        if (
    mail($to$subject$message$headers)){

            return 
    true;
            
        } else {
        
            return 
    false;
        }
    }

    function 
    passwordReset($email){

        if(
    $email != NULL)
        {
            
    //query to do something like select email from user(table) where email = $email
            
    $query_ch_email mysql_query("EDIT ME!!!!!!!!!!") or die(mysql_error());
            if (
    mysql_num_rows($query_ch_email) > 0)
            {
                
                
    //query to get userid ex: select * from user(table) where email = $email
                
    $query_get_userid mysql_query("EDIT ME!!!!!!!!!!") or die(mysql_error());
                
    $user_array mssql_fetch_assoc($query_get_userid);
                
    $userid $user_array['userid']; //not sure if this is correct
                
                //I dont think passwords are stored in the same table as the emails, pretty sure its login table so ex:
                //select * from login(table) where userid = $userid
                
    $query_get_password mysql_query("EDIT ME!!!!!!!!!!") or die(mysql_error());
                
    $login_array mssql_fetch_assoc($query_get_password);
                
    $password $login_array['Password']; //not sure if correct
                
                


                //Subject EDIT THIS
                
    $subject "GunZServer - Password Reset";

                
    //To / From
                /////////////////////////!! EDIT ME ////////////////////////////////////////
                
    $from "no-reply@yourwebsite.com";
                
    $to   $email;

                
    //Html Message EDIT THIS
                
    $message "<html>
                                <body> 
                                    Your <b>GunZServer</b> password is: <b>"
    .$pass."</b><br />
                                    - GunZServer
                                </body> 
                            </html>"



                if(
    sendEmail($to$email$subject$message))
                {
                    
    $msg "Password Sent, You should be Recieving it Soon!";
                    return 
    $msg;
                    
                } else {
                
                    
    $msg "Unable to Send Password at this Time. Please inform the administrator if this continues.";
                    return 
    $msg;

                }
            
            } else {
            
                
    $msg "Email Does Not Exists";
                return 
    $msg;

            }
        
        } else {
        
            return;
        }
    }
                   

    ?>
    Heres a quick script using the functions, with the html form. You can just include the stuff above with one of your function files, or something and below integrate it to your website how ever you please, I would but I have no idea what website files your using.
    PHP Code:
    <?php

    if(isset($_GET['do']))
    {
        if(
    $_GET['do'] == "sendEmail")
        {
            if(
    $_POST['email'] != NULL)
            {
                
    $email $_POST['email'];
                
                if(
    filter_var($emailFILTER_VALIDATE_EMAIL))
                {
                    
    $email anti_injection($email);
                    
    $msg passwordReset($email);
                    echo 
    '<script type="text/javascript">alert("' $msg '"); </script>';
                
                } else {
                
                    echo 
    '<script type="text/javascript">alert("Email is not Valid."); </script>';
                }
                    
            } else {
            
                echo 
    '<script type="text/javascript">alert("Email Field is Blank"); </script>';
            }
        }
    }
    ?>
    <B>Password Reset</b><br />
    <form name="input" action="<?php echo $_SERVER['PHP_SELF']; ?>?do=sendEmail" method="POST">
    Email: <input type="text" name="email">
    <input type="submit" value="Submit">
    </form>
    Make sure you read the comments so you understand where to edit like the $from and the queries, enjoy.
    However you will still need to fix your issues with your smtp server.
    - Secured.

    Edit 2:

    Wasn't paying any attention and used mysql_real_escape_string() =-=" well anyways I edited it to use a the include sql injection function, maybe not the best one but ya.

    Edit 3:
    Little Syntax Error,
    PHP Code:
                    passwordReset($email);
                    
    $msg passwordReset($email); 
    Should be:
    PHP Code:
                    $msg passwordReset($email); 
    Last edited by Secured; 10-10-12 at 05:46 AM.

  8. #8
    Die() Secured is offline
    MemberRank
    Sep 2011 Join Date
    /home/SDev/Location
    555Posts

    Re: [PHP] Email Function

    Quote Originally Posted by wesman2232 View Post
    I don't have access to the VPN that the script is hosted on. I just have the exact same files on my localhost, then send updates to the hoster. Which is why im using the ini_set function.
    I'm just confused on the part where it says "t@to.com", "f@from.com"
    'From', ok, but wouldnt the 'to' need a email grabbed from the DB and then set there so any of the users could reset their pass?
    (This is part of the Forgot Password function in the web)

    Is the mail server required to be on that VPN or can it still be defined with the ini_set function?
    Do you mean VPS? :x
    Are you sure you have permission / ini_set is allowed?

    Soz double post they normally merge X_X



Advertisement