Link to home
Start Free TrialLog in
Avatar of kenjpete
kenjpete

asked on

Including username and password in email dynamically

I am trying to figure out how to include a user's username and password in an email dynamically. I have an Access database that contains basic student information including first name, last name, username, password, email address, school and teacher. I want to query the database based on a parameter like teacher or school and then send an email to all students who meet that query. In the email I want to include the student's username and password. Here is my code:

**************
Query:
**************
<cfquery name="GetEmail_schools" datasource="#DataSource#">
  SELECT StudentID, First, Last, Email, School, Username, PCode
  FROM data
  WHERE School = '#session.School#' AND Email IS NOT NULL
</cfquery>

****************
Code to send email:
****************
 <cfmail
        Type="HTML"
        query="GetEmail_schools"
        To="#Email#"
        From="xxx@xxxx.com"
        Subject="Your login information"
        SERVER="127.0.0.1"
        PORT="25"
        >
            <p>#First#,</p>
                      <cfloop query="GetEmail_schools">
                          <cfoutput>
                            Your username is: #GetEmail_schools.Username# <br>
                            Your password is: #GetEmail_schools.Pcode#
                          </cfoutput><br>
                      </cfloop>
 
 </cfmail>
***************************
The result when I load this page is that everyone gets a list of all usernames and passwords? How do I structure my code so an email recipient only sees their username/password?

Ken

P.S. I also tried removing the <cfloop> tags and using just <cfoutput> but the result was the same.
ASKER CERTIFIED SOLUTION
Avatar of mkishline
mkishline

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Bennett
When you specify the query attribute in the cfmail tag, you do not need to use a loop, the cfmail will automatically loop through the query. you also do not need to use cfoutput withing a cfmail tag. you code should be like this:

**************
Query:
**************
<cfquery name="GetEmail_schools" datasource="#DataSource#">
  SELECT StudentID, First, Last, Email, School, Username, PCode
  FROM data
  WHERE School = '#session.School#' AND Email IS NOT NULL
</cfquery>

****************
Code to send email:
****************
 <cfmail
        Type="HTML"
        query="GetEmail_schools"
        To="#Email#"
        From="xxx@xxxx.com"
        Subject="Your login information"
        SERVER="127.0.0.1"
        PORT="25"
        > 
            <p>#GetEmail_schools.First#,</p>
                            Your username is: #GetEmail_schools.Username# <br>
                            Your password is: #GetEmail_schools.Pcode#<br>
 
 </cfmail>
***************************
lol, I guess I typed too slow =)
Avatar of mkishline
mkishline

but it's always nice to reinforce a good idea ;-)
Avatar of kenjpete

ASKER

Great! That appears to have worked perfectly. Now the question is who gets the 250 points? Can I divide it up evenly or do I have to take the solution that was dated first?
give it to mkishline since he gave the answer first.