We help IT Professionals succeed at work.

cfmail question

billythehamster
on
278 Views
Last Modified: 2013-12-24
Well here's another question about cfmail. I have used the following script for about 6 months using Access without any problems. However this is the first time I have tried it on a SQL database and it won't work. Can anybody please help.

<cfquery name="get_email" datasource="mydsn">
SELECT DISTINCT emailaddress FROM contact
WHERE preferredmethod <> 'Letter'
</cfquery>

<cftry>
<cfmail to="#emailaddress#" from="Me<me@me.com>" subject="#FORM.subject#" query="get_email" timeout="600">
<cfmailpart type="text">

#FORM.TEXT#

</cfmailpart>

<cfmailpart type="html">

#FORM.htmltext#

</cfmailpart>

</cfmail>

<cfcatch type="any">

</cfcatch>

</cftry>
Comment
Watch Question

Commented:
what error r u getting ?

i dont see a problem with the query - thou i didnt understand why have u used cfmailpart - when u cld have made the entire email as html -> type="html".

also since u r sending the email based on ur query - i wonder why "form." is being used there ? just asking

let me know ...

Author

Commented:
I wan't getting an error the mails just weren't going. However I have managed to solve the problem. Thanks
Quick warning, not sure if this was your problem but. By using the query param in cfmail if 1 email address is mallformed all emails after that email address will fail to send. You may want to consider puting the cfmail in a loop of the query instead.
If you solved the problem post your resolution so other might benifit from it and ask for a refund :)

Commented:
I'd ass ume it's most likely an issue with the mail server. Try going through cfadmin and testing your connection to it.

Author

Commented:
mmmm forums, nobody seems to actually read the question you put. no disrespect fozzynet but if you read my question it says that the only thing that has changes is the database. i've gone from access to sql why on earth would i need to play around with my mail server. if my question had said i have upgraded cf 5 to 6.1 and suddenly it's stop working or i changed my mail server then suddenly it stopped working your comment would have been helpful. please could admins get into the habit of reading questions before posting replys this is supposed to be an expert forum and i pay to be a member, normally i overlook this kind of thing but not when i am paying for it!

Author

Commented:
here is the code that works

<cfquery name="get_email" datasource="mydsn">
SELECT DISTINCT emailaddress FROM contact
WHERE preferredmethod <> 'Letter'
</cfquery>

</cfif>
<cfset MyMail = 0>
<cfloop query="get_email">

<cftry>
<cfmail to="#get_email.emailaddress#" from="me@me.com" subject="#FORM.subject#" timeout="600">
<cfmailpart type="text">

text here

</cfmailpart>

<cfmailpart type="html">

html here

</cfmailpart>
</cfmail>

<cfcatch type="any">

</cfcatch>

</cftry>
<cfset MyMail = (#MyMail# + 1)>
</cfloop>

Author

Commented:
hi anandkp

not everyone can read html mail that's why it is sent out as a multipart. #FORM# is being used becuase it is a bulk email list therefore the text filled in on the pervious page is what is sent out to the users. i use activeedit to generate the html version and a <textarea> field for the plain text version. hope this explain enough so that anyone wanting to use a bulk emailer can use this code. so far tested with 10,000 email addresses and no problems.
Yeah I ran into a simular problem but I would like to point out that your comment to fozzy was uncalled for and kind of ironic since your upgrade from access to SQL was irrelevent to your solution/problem.

Commented:
what version of CF r u using ?? there r ways to make ur email work in both html & non html mail clients !

let me know - if ur intersted ....

Author

Commented:
i am using 6.1mx but my solution does work with both html and non html clients. if you  know a better way then I will give it a blast.

thannx

Commented:
OK ... thats the right way to go abt using CFMX 6.1

but i still cant figure out ur original problem - of thigns not working after upgrade from Access to SQL.
i wonder if DSN & everything is set up fine & is pointing to the right DB

Can u provide more info wrt the error ur getting ...

Author

Commented:
There was no error it was simply that the emails never made it into the spool directory on the CF server. It was very bizzare and confused me no end. The DSN was and still is set up fine and it is pointing to the right DB. If it wasn't then my modified query wouldn't have worked. Thanks for the help though it's must appreciated.

Commented:
surprising - if the mail server is configured poroperly in CFAdmin - it has to access teh mail server ...
can u chk on this ...

Author

Commented:
yes it is configured correctly and does have access to the mail server, with the new query i can see the emails going into the spool folder of the cf server and then into the spool folder of the mail server.

Commented:
then i dont see a problem with CF or the change of DB from access to SQL - its a problem with ur email server

Author

Commented:
No it isn't becuase the old mailing list in access works with the old code, the sql mailing list won't work with the old code but will work with the new one. I haven't touched the mail server or the cf server.
What are you trying to figure out I told you what the problem was already. If you query 6 emails and the 3rd email was mallformed say it was bad email@somwhere.com the 4th and 5th email address will not get processed under his original code.

Commented:
ya shouldn't he do a cftry and cfcatch around that code?
If you do a cftry and catch around a mail tag that has the query parameter in the cfmail tag as soon as it errors it will get caught and the query will not get processed further. If you loop through the query and embed the cftry in the loop each error is caught independently so the whole query is processed.

Author

Commented:
As posted already my problem has been solved. By Me.

PE_CF_DEV you also seem to suffer from the same problem fozzynet has of not reading the post properly. Am I seeing things or does the code contain and has always contained <cftry> and <cfcatch>. Please think about your answers before posting.

Here is the problem, my code works I change the DB and it stops working. Conclusion changing the DB has broken it! I was hoping to find out why changing the DB caused it to break but I haven't. I hope you guys never decide to be mechanics -

billythehamster: hey mr mechanic I have a flat tyre.
mr mechanic: leave it with us

3 days later

billythehamster: where's my car
mr mechanic: yeah we removed the engine, stripped it down and rebuilt it but i'm afraid you still have a flat tyre.
billythehamster: did you think of changing the tyre
mr mechanic: there's nothing wrong with your tyre it's the engine
billythhamster: thanks mr mechanic you've been a great help.
Your Orginal code:
<cftry> *NOTICE THIS*
<cfmail to="#emailaddress#" from="Me<me@me.com>" subject="#FORM.subject#" query="get_email"  *NOTICE THIS*
timeout="600">
<cfmailpart type="text">

#FORM.TEXT#

</cfmailpart>

<cfmailpart type="html">

#FORM.htmltext#

</cfmailpart>

</cfmail>

<cfcatch type="any">

</cfcatch>

</cftry>


Your new code:

<cfquery name="get_email" datasource="mydsn">
SELECT DISTINCT emailaddress FROM contact
WHERE preferredmethod <> 'Letter'
</cfquery>

</cfif>
<cfset MyMail = 0>
<cfloop query="get_email"> *NOTICE THIS*

<cftry> *NOTICE THIS*
<cfmail to="#get_email.emailaddress#" from="me@me.com" subject="#FORM.subject#" timeout="600">
<cfmailpart type="text">

text here

</cfmailpart>

<cfmailpart type="html">

html here

</cfmailpart>
</cfmail>

<cfcatch type="any">

</cfcatch>

</cftry>
<cfset MyMail = (#MyMail# + 1)>
</cfloop>

What you will notice is that I have said the same thing twice and that I was right. You have moved the query to a loop and embeded the cftry in the loop.
"Here is the problem, my code works I change the DB and it stops working. Conclusion changing the DB has broken it!"

That is a false conclusion.  A valuable leasson to learn is that code does not always function they way you think it does. After you make a change it is not always that change that is causing the problem.

Author

Commented:
Ok if you say so. But to bring this matter to a conclusion for anyone else with the same problem the code above works. I managed to solve it by using my false conclusions.
How was the problem solved? Your query to the SQL server remained the same?

Author

Commented:
it is different if you read the post, i introduced a loop and changed <cfmail to="#emailaddress#" from="Me<me@me.com>" subject="#FORM.subject#" query="get_email" timeout="600"> to <cfmail to="#get_email.emailaddress#" from="me@me.com" subject="#FORM.subject#" timeout="600">

Author

Commented:
bizzare but it worked, I don't know why because it is doing the same thing but written differently. The loop on it own wasn't enough to solve the problem maybe someone who understands how Coldfusion communicates with SQL might be able to shed some light on it?
Changing databases will not require you to loop through the query as opposed to puting the query in the cfmail tag attribute query.  What you did change though is how your cftry is functioning.

By placing the query as an attribute of cfmail you are telling coldfusion hey if this email messes up catch the error. So coldfusion gets all the emails ready. If it errors Coldfusion catches that error sends the emais it has ready, anything that was not ready would not be sent.

By looping through your query Coldfusion prepares each email seperately. by placing your cftry inside the loop you are telling coldfusion for each itteration of the loop catch the error for me. So if an error orccurs the error is caught and coldfusion continues on to the next itteration.

To further explain this try putting your <cftry> and catch around the loop you should find it functions simularly to your orginal code. By doing this you are telling coldfusion to execute the loop once an error occurs though the loop will not finish it will error out and be caught.
I object if you read his repsonse.
>>is different if you read the post, i introduced a loop and changed <cfmail to="#emailaddress#" from="Me<me@me.com>" subject="#FORM.subject#" query="get_email" timeout="600"> to <cfmail to="#get_email.emailaddress#" from="me@me.com" subject="#FORM.subject#" timeout="600">

I offer that suggestion multiple times...I beleive I deserve the points. If not I certaintly do not agree with any points refund.

Commented:
If you read you notice that he posted that he solved the problem and posted the solution right away.  It was after that point that you offered a suggestion, but he had already had one that worked and had posted it.
i see where you are talking about...I guess I withdraw my objection...
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.