Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to send out a record by email automatically

Author  Topic 

lw1990
Yak Posting Veteran

85 Posts

Posted - 2009-11-23 : 16:17:37
Hi,
I have a database table, the records in the table are updated periodically (maybe everyday, maybe twice a week), but when the records updated, I need to send an email only to that people listed in the table with his/her contain information.
The table has the information of the people and the email address:

Name Email Short_Message

AA aa@myfirm.com Contact customer
BB bb@myfirm.com Client needs help

So, when the table is updated:
One email will be sent to AA(aa@myfirm.com) with a short message (Contact customer);
One email will be sent to BB(bb@myfirm.com) with a short message (Client XYZ needs help)...

How can I get this approach?

Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-23 : 18:17:51
You can send email via Database Mail. Write a SQL job that periodically reads your table and then sends data to them. Hopefully you have a column in your table that you can use to determine which rows have been emailed out and which ones haven't.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

lw1990
Yak Posting Veteran

85 Posts

Posted - 2009-11-23 : 18:55:19
Thank you Tara,
It looks like I have to use "sp_send_dbmail" to send email. I don't have to worry about the which records I need to send as I'll "Drop the table" to delete all records after email sent out.
The problem is that how I can use @recipients = select email from infotable, and loop through the whole table to send all records out.

Thanks.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-23 : 18:57:34
Use a WHILE loop to get each email adress. In the WHILE loop and after you get the email address, call sp_send_dbmail with that specific email address.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

lw1990
Yak Posting Veteran

85 Posts

Posted - 2009-11-25 : 12:23:28
Thank you Tara for the response.
Now the question is how to pass the selected variable to the @recipients.
Surely if I use
EXEC msdb.dbo.sp_send_dbmail
@recipients='aa@mycompany.com',@body=...
There is no problem to send out the email.

But if I use
DECLARE @emailaddress varchar(100)
select @emailaddress = select email from infotable ...
and pass the email address to Database Mail:
EXEC msdb.dbo.sp_send_dbmail
@recipients=’@emailaddress’,@body=...

I’ll get error message from mail log:
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 3 (2009-11-24T12:03:47). Exception Message: Cannot send mails to mail server. (The specified string is not in the form required for an e-mail address.).)

How to get the value fro the table and pass to the @recipients in Database Mail?

Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-25 : 12:58:34
You've got single quote around the @emailaddress variable, which isn't allowed.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -