本文介绍了使用CFScript时,为什么CFMAIL忽略查询的第一行以外的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CFScript CFC中使用CFmail。我希望CFMail遍历查询,并根据查询内容更改值。只要查询对象只有1行,它就可以做到这一点。如果它有多行(例如5行),它将发送5封电子邮件,但是每封电子邮件都将包含来自第一个查询行的值。我尝试了几种不同的方法。见下文:

I'm trying to use CFmail in a CFScript CFC. I want CFMail to iterate through a query, and change values based on the query contents. It does this just fine, as long as the query object only has 1 row. If it has multiple rows (e.g. 5 rows), it will send 5 emails, but each email will contain the values from the first query row. I've tried a few different things. See below:

查询对象:

Name    |     Email     |   Number
----------------------------------
John    |  john@foo.com |     12
Bill    |  bill@bar.com |     42
Ann     |  ann@bat.com  |     100

CFScript:

var mailerService = new mail();

mailerService.setQuery(nameEmailNumberQuery);
mailerService.setTo('eterps@sendtomefortesting.com');
mailerService.setFrom( 'noReply@example.com' );
mailerService.setSubject('Hi');
mailerService.setFailto('fail@foo.com');
mailerService.setType('html');
mailerService.setSpoolenable(true);

    savecontent variable="mailBody"{
    WriteOutput(
        "Hello " & Name & ". Your number is: " & Number & "!"
    );
}

mailerService.send(body=mailBody & tmpVariable);

使用上面的代码,我收到了三封电子邮件。每封电子邮件都说:约翰,您好。您的电话号码是:12!

Using the above code, I get three emails. Each email says, "Hello John. Your number is: 12!"

我也尝试过:

WriteOutput(
    "Hello "
     & mailerService.getQuery().Name
     & ". Your number is: "
     & mailerService.getQuery().Number
     & "!"
);

和:

WriteOutput(
    "Hello "
    & mailerService.getQuery().Name[mailerService.getQuery.CurrentRow]
    & ". Your number is: "
    & mailerService.getQuery().Number[mailerService.getQuery.CurrentRow]
    & "!"
);

编辑:我尝试了几件事(@invertedSpear建议)

A couple more things I've tried (One as suggested by @invertedSpear)

在电子邮件正文中使用查询文字:

Using the Query literal in the email body:

WriteOutput(
    "Hello "
    & nameEmailNumberQuery.Name
    & ". Your number is: "
    & nameEmailNumberQuery.Number
    & "!"
);

尝试使用递增计数器:

var counter = 1;
...
WriteOutput(
    "Hello "
    & mailerService.getQuery().Name[counter]
    & ". Your number is: "
    & mailerService.getQuery().Number[counter]
    & "!" & evaluate('counter = counter++')
);

每次都具有相同的结果-3封电子邮件,全部带有 John和 12。倾销 mailerService.getQuery()。CurrentRow 会导致每封电子邮件为 1。我在Windows Server 2008 R3上使用Coldfusion 9.0.1。

With the same result each time - 3 emails, all with 'John' and '12'. Dumping mailerService.getQuery().CurrentRow results in '1' for each email. I am using Coldfusion 9.0.1 on Windows Server 2008 R3.

推荐答案

在这种情况下,您需要进行循环自己,然后为循环中的每个项目调用 .send()方法。 MailService不会为您执行循环。因此,您需要执行以下操作:

In this case, you'll need to do the looping yourself, and call the .send() method for each item in the loop. The MailService won't do the looping for you. So you need to do this:

var mailerService = new mail();

mailerService.setTo('eterps@sendtomefortesting.com');
mailerService.setFrom( 'noReply@example.com' );
mailerService.setSubject('Hi');
mailerService.setFailto('fail@foo.com');
mailerService.setType('html');
mailerService.setSpoolenable(true);

for (x = 1; x <= nameEmailNumberQuery.RecordCount; x=x+1) {
    savecontent variable="mailBody"{
        WriteOutput(
            "Hello " & nameEmailNumberQuery.Name[x] & ". Your number is: " & nameEmailNumberQuery.Number[x] & "!"
        );
    }
    mailerService.send(body=mailBody & tmpVariable);
}

这将允许您一次配置基本电子邮件属性,然后发送该行中每个项目的单独电子邮件。如果每个电子邮件都应该转到一个唯一的电子邮件地址,只需将 setTo()方法移至循环中,并引用该电子邮件地址的相应列即可。

That will allow you to configure the base email properties once, and then send a separate email for each item in the row. If each email should go to a unique email address, just move the setTo() method into the loop and reference the proper column for the email address.

这篇关于使用CFScript时,为什么CFMAIL忽略查询的第一行以外的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:55
查看更多