Mail function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
enemeth
Forum Commoner
Posts: 66
Joined: Tue Mar 27, 2007 8:55 am

Mail function

Post by enemeth »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi there , 

i  am having a problem with my registration form on my site, it adds all the information into the database just fine , im happy about that , but it never emails the confirmation , can someone tell me what i am doing wrong ? is there something more i must do ?  here is the code

Code: Select all

<? 
include 'db.php'; 
// Define post fields into simple variables 
$first_name = $_POST['first_name']; 
$last_name = $_POST['last_name']; 
$email_address = $_POST['email_address']; 
$username = $_POST['username']; 
$info = $_POST['info']; 
/* Let's strip some slashes in case the user entered 
any escaped characters. */ 
$first_name = stripslashes($first_name); 
$last_name = stripslashes($last_name); 
$email_address = stripslashes($email_address); 
$username = stripslashes($username); 
$info = stripslashes($info); 
/* Do some error checking on the form posted fields */ 
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){ 
    echo 'You did not submit the following required information! <br />'; 
    if(!$first_name){ 
        echo "First Name is a required field. Please enter it below.<br />"; 
    } 
    if(!$last_name){ 
        echo "Last Name is a required field. Please enter it below.<br />"; 
    } 
    if(!$email_address){ 
        echo "Email Address is a required field. Please enter it below.<br />"; 
    } 
    if(!$username){ 
        echo "Desired Username is a required field. Please enter it below.<br />"; 
    } 
    include 'form.htm'; // Show the form again! 
    /* End the error checking and if everything is ok, we'll move on to 
     creating the user account */ 
    exit(); // if the error checking has failed, we'll exit the script! 
} 
/* Let's do some checking and ensure that the user's email address or username 
does not exist in the database */ 
 $sql_email_check = mysql_query("SELECT email_address FROM users  
            WHERE email_address='$email_address'"); 
 $sql_username_check = mysql_query("SELECT username FROM users  
            WHERE username='$username'"); 
 $email_check = mysql_num_rows($sql_email_check); 
 $username_check = mysql_num_rows($sql_username_check); 
 if(($email_check > 0) || ($username_check > 0)){ 
    echo "Please fix the following errors: <br />"; 
    if($email_check > 0){ 
        echo "<strong>Your email address has already been used by another member 
        in our database. Please submit a different Email address!<br />"; 
        unset($email_address); 
    } 
    if($username_check > 0){ 
        echo "The username you have selected has already been used by another member 
         in our database. Please choose a different Username!<br />"; 
        unset($username); 
    } 
    include 'form.htm'; // Show the form again! 
     exit();  // exit the script so that we do not create this account! 
 } 
/* Everything has passed both error checks that we have done. 
It's time to create the account! */ 
/* Random Password generator. 
http://www.phpfreaks.com/quickcode/Rand ... tor/56.php 
We'll generate a random password for the 
user and encrypt it, email it and then enter it into the db. */
function makeRandomPassword() { 
  $salt = "abchefghjkmnpqrstuvwxyz0123456789"; 
  srand((double)microtime()*1000000);  
      $i = 0; 
      while ($i <= 7) { 
            $num = rand() % 33; 
            $tmp = substr($salt, $num, 1); 
            $pass = $pass . $tmp; 
            $i++; 
      } 
      return $pass; 
} 
$random_password = makeRandomPassword(); 
$db_password = md5($random_password); 
// Enter info into the Database. 
$info2 = htmlspecialchars($info); 
$sql = mysql_query("INSERT INTO users (first_name, last_name, 
        email_address, username, password, info, signup_date) 
        VALUES('$first_name', '$last_name', '$email_address', 
        '$username', '$db_password', '$info2', now())")  
        or die (mysql_error()); 
if(!$sql){ 
    echo 'There has been an error creating your account. Please contact the webmaster.'; 
} else { 
    $userid = mysql_insert_id(); 
    // Let's mail the user! 
    $subject = "Your Membership at The Truth Discovered!"; 
    $message = "Dear $first_name $last_name, 
    Thank you for registering at our website, http://www.thetruthdiscovered.com! 
    You are two steps away from logging in and accessing our exclusive members area. 
    To activate your membership, 
    please click here: http://www.thetruthdiscovered.com/activ ... b_password 
    Once you activate your memebership, you will be able to login 
    with the following information: 
    Username: $username 
    Password: $random_password 
    Thank You 
    The Staff  
    This is an automated response, please do not reply!";  
    mail($email_address, $subject, $message,  
        "From: The Truth Discovered Webmaster<admin@thetruthdiscovered.com>\n 
        X-Mailer: PHP/" . phpversion()); 
    echo 'Your membership information has been mailed to your email address! 
    Please check it and follow the directions!'; 
} 
?>
i got this code from a tutorial and it is wonderful , i disected it from the tutorial and found out what it all does , i am very new to php and it is sinking in , but i just cant find out why it will not email , any help?

Thx ;)

Elaine


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The first thing to check is if that piece of code is actually executing. Are any errors being returned by the script?
lanasa
Forum Newbie
Posts: 14
Joined: Mon Mar 26, 2007 7:49 am
Location: Buffalo, NY

Re: Mail function

Post by lanasa »

First, verify that there isn't an issue with the sendmail program (if a linux server).

Otherwise, maybe try the following in the From parameter:

"From: The Truth Discovered Webmaster<admin@thetruthdiscovered.com>\r\n"
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can always check against the mail() function, as it returns true/false when called...

Code: Select all

<?php
if (!mail(...))
{
    echo 'there was a problem';
}
?>
Of course, that little snippet needs a lot more error checking in it, but just a simple check against the mail function should get you started. Also, you may want to make sure that the mail application you are using is not trashing the message as spam, which is sometimes the case in situations like this.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

There should be a space here:

From: The Truth Discovered Webmaster<admin@thetruthdiscovered.com>

Should be:

From: The Truth Discovered Webmaster <admin@thetruthdiscovered.com>
enemeth
Forum Commoner
Posts: 66
Joined: Tue Mar 27, 2007 8:55 am

Figured it out!

Post by enemeth »

Hi there again :)

thank you for your hard work and thoughtfulness, i found out that the hosting site doesnt give the mail() function with the free hosting , so i am obliged to pay for hosting :) oh well , unless any of you know of any good free hosting that includes the mail function ??? hehehe im trying ! ;)

Elaine
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I don't know any freebies, but I do know that GoDaddy has a linux hosting account that costs about $30 USD per year and it has a lot of features.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

http://www.swiftmailer.org/

Doesn't require the mail() function, but your host may block attempts to use that too. Give it a shot :)
Post Reply