SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why can't I put variables in the send to in my email processing page??

    I am trying to include variables in the send, and the subject, but when i do it doesn't seem to want to send.. I have the following code:

    PHP Code:
     <?php
    mysql_select_db
    ('1234'$connection);

    $usernamesub '$_SESSION[user_name]';

    $enddate mysql_query("SELECT sunday FROM dateconfig");

    $toemail1 mysql_query("SELECT email FROM timesheet
    WHERE username='
    $_SESSION[user_name]");

    $departmentemail mysql_query("SELECT department FROM timesheet
    #WHERE username='
    $_SESSION[user_name]")';

    ?>



    <?php
    //define the receiver of the email
    $to = '
    testing@testing.com,$toemail1';
    //define the subject of the email
    $subject = '
    WEEEEEEEEE $usernamesub';

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You are not fetching any of the results your are querying db for!

    After each mysql_query, you should call mysql_fetch_assoc to get the results to a var.
    -- Jelena --

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show me and example?

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it be something like this?

    PHP Code:
    <?php 
    mysql_select_db
    ('1234'$connection); 

    $usernamesub '$_SESSION[user_name]'
    mysql_fetch_assoc 

    $enddate 
    mysql_query("SELECT sunday FROM dateconfig"); 
    mysql_fetch_assoc 

    $toemail1 
    mysql_query("SELECT email FROM timesheet 
    WHERE username='
    $_SESSION[user_name]"); 
    mysql_fetch_assoc 

    $departmentemail 
    mysql_query("SELECT department FROM timesheet 
    WHERE username='
    $_SESSION[user_name]")'; 
    mysql_fetch_assoc 

    ?> 



    <?php 
    //define the receiver of the email 
    $to = '
    testing@testing.com,$toemail1'; 
    //define the subject of the email 
    $subject = '
    WEEEEEEEEE $usernamesub';

  5. #5
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $db 
    mysql_select_db('1234'$connection);

    $usernamesub $_SESSION["user_name"];


    $enddate mysql_query("SELECT sunday FROM dateconfig") or die("Something went wrong. MySQL said:".mysql_error());

    //do you expect one or more than one result here??
    //if it's only one, use mysql_fetch_assoc as bellow. If more, use loop to gather all results


    $select_email mysql_query("SELECT email FROM timesheet WHERE username='".$_SESSION['user_name']."'");

    if(
    mysql_num_rows($result) != 0)
    {
     
    $toemail1 mysql_fetch_assoc($select_email);
    }


    $departmentemail mysql_query("SELECT department FROM timesheet WHERE username='$_SESSION[user_name]'");

    //do the same here

    ?>


    <?php

    //define the receiver of the email

    $to 'testing@testing.com, $toemail1["email"]';

    //define the subject of the email

    $subject 'WEEEEEEEEE - $usernamesub';
    -- Jelena --

  6. #6
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its still not sending.. here is my revised code:

    PHP Code:
    <?php
    $db 
    mysql_select_db('1234'$connection);

    $usernamesub $_SESSION["user_name"];

    $enddate mysql_query("SELECT sunday FROM dateconfig") or die("Something went wrong. MySQL said:".mysql_error());

    if(
    mysql_num_rows($result) != 0)
    {
    $enddateemail mysql_fetch_assoc($enddate);
    }

    $select_email mysql_query("SELECT email FROM timesheet WHERE username='".$_SESSION['user_name']."'");

    if(
    mysql_num_rows($result) != 0)
    {
    $toemail1 mysql_fetch_assoc($select_email);
    }

    $departmentemail mysql_query("SELECT department FROM timesheet WHERE username='$_SESSION[user_name]'");

    if(
    mysql_num_rows($result) != 0)
    {
    $departmentemail1 mysql_fetch_assoc($departmentemail);
    }

    ?>

    <?php
    //define the receiver of the email
    $to 'testing@testing.net,$toemail1["email"]';
    //define the subject of the email
    $subject 'Timesheet - $usernamesub - $departmentemail1["department"] - $enddateemail["sunday"]';

  7. #7
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried to var_dump variable toemail1 to see if anything is in there?
    -- Jelena --

  8. #8
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by var_dump?

    I know there is something in that cell.

  9. #9
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First of, have you tried to run the sql query directly in phpmyadmin or from command line to see if actually returns any results?

    var_dump

    When you are not sure about what is in the variable, always try to echo it or if it's an array, use var_dump to see what it contains.
    -- Jelena --

  10. #10
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All the queries are returning the right things. I think it may be a syntax error in the code somewhere, because it's displaying a blank page no matter what

  11. #11
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As far as I can see there is no syntax error.
    are you actually calling mail function?
    Aslo make sure that subject doesn't contain any new lines.
    -- Jelena --

  12. #12
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    may I ask what you mean by calling mail function?

  13. #13
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I understood you are trying to email those details from the database?
    -- Jelena --

  14. #14
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right

  15. #15
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't see in the code you provided that you are actually calling mail function which sends email.
    -- Jelena --

  16. #16
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't post all the code because I knew that it worked without the variables but here is the rest of the code:

    PHP Code:
    <?php
    //define the receiver of the email
    $to 'test@test.net,$toemail1["email"]';
    //define the subject of the email
    $subject 'Timesheet - $usernamesub - $departmentemail1["department"] - $enddateemail["sunday"]'
    //create a boundary string. It must be unique 
    //so we use the MD5 algorithm to generate a random hash
    $random_hash md5(date('r'time())); 
    //define the headers we want passed. Note that they are separated with \r\n
    $headers "From: test@test.net\r\nReply-To: test@test.net";
    //add boundary string and mime type specification
    $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""
    //define the body of the message.
    ob_start(); //Turn on output buffering
    ?>
    --PHP-alt-<?php echo $random_hash?>  
    Content-Type: text/plain; charset="iso-8859-1" 
    Content-Transfer-Encoding: 7bit

    Hello World!!! 
    This is simple text email message. 

    --PHP-alt-<?php echo $random_hash?>--

    <?
    //copy current buffer contents into $message variable and delete current output buffer
    $message ob_get_clean();
    //send the email
    $mail_sent = @mail$to$subject$message$headers );
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    echo $mail_sent "Mail sent" "Mail failed";
    ?>

  17. #17
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried to echo $to and $subject to see if everything is ok in there?
    -- Jelena --

  18. #18
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly, is error_reporting turned to E_ALL and display_errors set to true? There might be an error and it's just not displaying.

    Secondly, as suggested, use var_dump() to check your variables contain what they are supposed to.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  19. #19
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can get anything to show at all on that page... It's just blank..

    I'll do

    <?php
    echo 'hello world';
    ?>

    at the top of the page and it won't show anything at all

  20. #20
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when I take out the

    PHP Code:
    <?php
    $db 
    mysql_select_db('1234'$connection);

    $usernamesub $_SESSION["user_name"];

    $enddate mysql_query("SELECT sunday FROM dateconfig") or die("Something went wrong. MySQL said:".mysql_error());

    if(
    mysql_num_rows($result) != 0)
    {
    $enddateemail mysql_fetch_assoc($enddate);
    }


    $select_email mysql_query("SELECT email FROM authorize WHERE username='$_SESSION['user_name']'");

    if(
    mysql_num_rows($result) != 0)
    {
    $toemail1 mysql_fetch_assoc($select_email);
    }


    $departmentemail mysql_query("SELECT department FROM authorize WHERE username='$_SESSION[user_name]'");

    if(
    mysql_num_rows($result) != 0)
    {
    $departmentemail1 mysql_fetch_assoc($departmentemail);
    }

    ?>
    And also the variables in the subject, and to..

    It works fine.

  21. #21
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,183
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    vars in quotes

    It looks like you have variables inside single quotes. These will be parsed as strings not variables. Maybe this is what's breaking the code?
    Does the echo show the variable name or it's value?

  22. #22
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it shows the value...

    So how would I show variables in the to and subject?

  23. #23
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,183
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    2 ideas

    Try changing
    PHP Code:
    //define the receiver of the email 
    $to 'test@test.net,$toemail1["email"]'
    //define the subject of the email 
    $subject 'Timesheet - $usernamesub - $departmentemail1["department"] - $enddateemail["sunday"]'
    to
    PHP Code:
    //define the receiver of the email 
    $to 'test@test.net'
    //define the subject of the email 
    $subject 'Timesheet test'
    just to see if it then works. If not, it may be the buffer code causing a problem. Try
    PHP Code:
    $message '--PHP-alt-' $random_hash '--\r\n';
    $message .= 'Content-Type: text/plain; charset="iso-8859-1"\r\n';
    $message .= 'Content-Transfer-Encoding: 7bit\r\n';
    $message .= 'Hello World!!!\r\n';
    $message .= 'This is simple text email message.\r\n';
    $message .= '--PHP-alt-' $random_hash '--'

  24. #24
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works when I just have standard text in the to and the subject, it's just when you start adding variables in there is where it's giving me a problem

  25. #25
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if I did something like

    $to = "'timesheet - '$usernamesub";


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •