• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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.
0
kenjpete
Asked:
kenjpete
  • 3
  • 2
1 Solution
 
mkishlineCommented:
Since specifying the query attribute in the cfmail tag is the equivalent of looping, you should be able to generate the email without the <cfloop> or <cfoutput query="">:

<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>
            Your username is: #GetEmail_schools.Username# <br />
            Your password is: #GetEmail_schools.Pcode#
 </cfmail>
0
 
Scott BennettCommented:
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>
***************************
0
 
Scott BennettCommented:
lol, I guess I typed too slow =)
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
mkishlineCommented:
but it's always nice to reinforce a good idea ;-)
0
 
kenjpeteAuthor Commented:
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?
0
 
Scott BennettCommented:
give it to mkishline since he gave the answer first.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now