[Zope] Iterating inside Sendmail

Dylan Reinhardt zope@dylanreinhardt.com
Thu, 21 Nov 2002 17:15:11 -0800


The prefix attribute of the <dtml-in> tag renames the sequence-specific 
variables within the loop.

Thus:

<dtml-in some_list>
   <dtml-var sequence-index> : <dtml-var sequence-item><BR>
</dtml-in>

produces the same thing as:

<dtml-in some_list prefix=list>
   <dtml-var list_index> : <dtml-var list_item><BR>
</dtml-in>

The second form substitutes list_index for sequence-index, list_item for 
sequence-item, etc.

The prefix attribute isn't required for a simple loop, but should (IMO) be 
considered a best practice.  It becomes *necessary* when you nest loops or 
want to make use of a sequence variable in an expression... in those cases, 
you'll be glad you're in the habit of using prefix and don't have to figure 
out why <dtml-var "do_something(sequence-item)"> is unable to bind the name 
"sequence".  (In an expression, "sequence-item" means "the sequence 
variable minus the item variable" not "the variable called sequence-item")

For more info, see the docs for dtml-in.

HTH,

Dylan

At 04:23 PM 11/21/2002, you wrote:
>Thanks Dylan,
>
>I have to run to a meeting tonight so I'll have to write back later to let
>you know how it goes.  I understand the first call, I'm making an empty list.
>then I'm bringing the Catalog in and appending the email addresses to the
>list.  The part I'm not sure about is the "prefix=recipient" and
>(recipient_item).  Everything else seems to make sense.  The dtml-var makes
>the list into a string that is comma delimited.  Thanks for the info, at
>least I know what the issue is now.
>
>Michael
>
>On Thursday 21 November 2002 03:19 pm, Dylan Reinhardt wrote:
> > This isn't a DTML problem, per se.  You can't send e-mail that isn't
> > properly formatted... that's your core problem.
> >
> > Unless the variable you render in the BCC field contains a value that
> > conforms with RFC822, you won't get the results you're expecting.
> >
> > Multi-line DTML statements insert line breaks into your output stream...
> > that's not a problem when they're forming HTML, but it will mess up e-mail
> > headers.  And when you're sending to multiple recipients (on any header),
> > bear in mind that the correct format is comma delimited, not space
> > delimited.
> >
> > Try something like:
> >
> > <dtml-call "REQUEST.set(my_names, [])">
> > <dtml-in "something_that_produces_addresses()" prefix=recipient>
> >    <dtml-call "my_names.append(recipient_item)">
> > </dtml-in>
> >
> > and then in <dtml-sendmail ...>, put:
> >
> > Bcc: <dtml-var "','.join(my_names)">
> >
> > HTH,
> >
> > Dylan
> >
> > At 01:42 PM 11/21/2002, you wrote:
> > >Thanks Tino,
> > >
> > >I guess this is where dtml just doesn't cut it.  i'm real new to Python,
> > >(just learning it), so I'll try working on this for a while and see if it
> > >does it or not.  I am just confused as to why dtml-in does not work inside
> > > of sendmail.  I also tried, as Mike suggested, creating another method
> > > and moving the [with / in] statement to it and then calling it as
> > >Bcc: <dtml-var listNames> and that didn't work either.  When I 'viewed'
> > > it, it appeared to work fine, ie: email@address1.com email@address2.com
> > > email@address.com etc. but not inside of sendmail.
> > >
> > >Michael
> > >
> > >On Thursday 21 November 2002 02:10 pm, Tino Wildenhain wrote:
> > > > Hi Michael,
> > > >
> > > > just drop DTML for this too and use
> > > > Mailhost.send() instead. If you have a recent
> > > > Zope, it strips BCC automatically and you only have
> > > > to provide it with mailtxt, which is mailheader + 1 free line +
> > > > mailbody.
> > > > Minimal mailheader is
> > > > To: Person <person@target.com>
> > > > From: Person <person@source.com>
> > > > Subject: what to say...
> > > >
> > > > The simplest way to do this in a loop is to use a Python long string:
> > > >
> > > > msg="""To: %(to)s
> > > > From: %(from)s
> > > > Subject: Your subject
> > > >
> > > > Hi there,
> > > > foobar...
> > > > """.replace('\n','\r\n')
> > > >
> > > > The replace stepp changes the line endings from single newline (Unix,
> > > > python) to
> > > > carriage-return + newline, which is for rfc822 complience.
> > > >
> > > > all_addresses=[{'to':'Person1 <person1@person1.com>,'from':'Me
> > > > <myself@me.com>'},
> > > >                {'to':'Person2 <person2@person2.com>,'from':'Me
> > > > <myself@me.com>'},
> > > >                {'to':'Person1 <person3@person3.com>,'from':'Me
> > > > <myself@me.com>'}]
> > > >
> > > > for target in all_addresses:
> > > >    Mailhost.send(msg % target)
> > > >
> > > > This is untested, but schould give you a starting point.
> > > >
> > > > Does this help?
> > > >
> > > > Regards
> > > > Tino Wildenhain
> > > >
> > > > --On Donnerstag, 21. November 2002 13:38 -0700 Michael
> > > >
> > > > <michael@nichestaffing.com> wrote:
> > > > > I have put up a small job site for Linux jobs here in N. Colorado.  I
> > > > > want to  offer the option of entering an email address and have job
> > > > > postings emailed  directly to subscribers as they are posted.  I
> > > > > tried the example below, but  there seems to be a problem iterating
> > > > > inside sendmail.
> > > > >
> > > > > For the time being, I just created a method and manually entered all
> > > > > the email addresses in it, ie: Bcc: <dtml-var listNames>, but I would
> > > > > like to automate it if possible.  Does anyone have any idea on how to
> > > > > approach this?
> > > > >
> > > > > Michael