【问题标题】:send multiple emails using cfmail使用 cfmail 发送多封电子邮件
【发布时间】:2024-01-09 21:06:01
【问题描述】:

我知道我在这里遗漏了一些非常小的东西,但我无法解决这个问题。我有以下疑问。 我需要 3 封电子邮件才能发送到各自的电子邮件,但发生的事情只是相同的电子邮件(内容)发送给 3 个用户。 知道这里出了什么问题。

<cfquery name = getitems >
Select items, id, users
 from table1
</cfquery>

This below query returns say 3 users
<cfquery name = getusers >
Select name,email,id from table2
</cfquery>

<cfloop query=”getusers”>
<cfquery name = getuserdata dbtype=”query” >
Select * from getitems where id=#id#
</cfquery>
</cfloop>

<cfsavecontent variable=”test”>
<cfloop query=" getuserdata ">
    <cfloop from="1" to="#arrayLen(itemsarray)#" index="ii">
Build the email body
</cfloop>
</cfloop
</cfsavecontent>

<cfloop query=”getusers”>
<cfmail >
Send email to users
</cfmail>
</cfloop>

【问题讨论】:

  • 为什么要在单独的循环中构建电子邮件正文而不是在发送时?

标签: coldfusion


【解决方案1】:

这是一个更好的方法。

<cfquery name="theOnlyQueryYouShouldNeed">
select name, email, etc
from table1 join table2 on table1.id = table2.id
etc
</cfquery>

<cfmail query="theOnlyQueryYouShouldNeed"
to="#email#
etc>
build body here.  You can output variables with pound signs
only, no cfoutput tag required
</cfmail>

【讨论】:

    【解决方案2】:

    Dan 指出的方法是更好的做事方式,但我会指出您的代码出了什么问题。 您运行循环并使用 savecontent 将所有值存储在单个变量中。现在您再次运行循环并将相同的变量发送给所有用户。

    <cfquery name = getitems >
     Select items, id, users from table1
    </cfquery>
    
    <cfquery name = getusers >
     Select name,email,id from table2
     </cfquery>
    
    <cfloop query=”getusers”>
      <cfquery name = getuserdata dbtype=”query” >
         Select * from getitems where id=#id#
     </cfquery>
    <cfsavecontent variable=”test”>
     <cfloop query=" getuserdata ">
       <cfloop from="1" to="#arrayLen(itemsarray)#" index="ii">
     Build the email body
    </cfloop>
    </cfloop>
       </cfsavecontent>
    <!--- so basically, you need to send email within the same loop in which you are generating your savecontent variable--->
     <cfmail >
       Send email to users
      </cfmail>
    </cfloop>
    

    【讨论】:

      最近更新 更多