|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 3:16 PM
Points: 2,749,
Visits: 1,405
|
|
We have looked at sending a newsletter type HTML mail and are looking at the sp_makewebtask and sp_runwebtask stored procedures.
In testing we have used these procedures to generate e-mails as files then have use xp_cmdshell to copy them to the Pickup directory of the SMTP server.
LinkedIn Profile
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Friday, March 08, 2013 11:15 AM
Points: 440,
Visits: 1,785
|
|
Here's my stab at it.
< /* Created by free online sql formatter: http://www.wangz.net/gsqlparser/sqlpp/sqlformat.htm */
/*This sets up a quick test table*/ IF object_id('DJmailDetails_T') > 0 DROP TABLE djmaildetails_t
CREATE TABLE djmaildetails_t ( id INT NOT NULL IDENTITY( 1 , 1 ), firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, emailaddress VARCHAR(100) NOT NULL, sendmail BIT NOT NULL DEFAULT (0) )
INSERT INTO djmaildetails_t (firstname, lastname, emailaddress, sendmail) VALUES ('dave', 'jackson', 'me@nonworkingExample.co.uk', 1)
INSERT INTO djmaildetails_t (firstname, lastname, emailaddress, sendmail) VALUES ('fred', 'bloggs', 'you@nonworkingExample.co.uk', 1)
INSERT INTO djmaildetails_t (firstname, lastname, emailaddress, sendmail) VALUES ('joe', 'smith', 'him@nonworkingExample.co.uk', 0)
INSERT INTO djmaildetails_t (firstname, lastname, emailaddress, sendmail) VALUES ('mike', 'jones', 'her@nonworkingExample.co.uk', 1) /*This selects from above table*/
DECLARE @recipients VARCHAR(4000)
SELECT @recipients = Coalesce(@recipients + ';','') + emailaddress FROM djmaildetails_t WHERE sendmail = 1
PRINT @recipients -- show what we got
PRINT '' -- a blank line /***************************************************************************************************/ /*The next two variables are normally set in theerror handling of each 'bit' of DML in your SP/Script*/
DECLARE @Error_status INT
DECLARE @Error_message VARCHAR(128)
SET @Error_status = -1
SET @Error_message = 'failure updating something!' --something apposite /*This should be in the error handler of the script or SP*/
ERRORHANDLER: DECLARE @sql VARCHAR(8000)
DECLARE @mailMessage VARCHAR(128)
DECLARE @mailSubject VARCHAR(128)
DECLARE @Procedure_Name SYSNAME -- Holds the name of the currently executing procedure SET @Procedure_Name = Object_name(@@PROCID) -- Set the procedure name in the variable
/*the next two lines would not be called above normally, so needs to be commented out here. declare @recipients varchar(4000) Select @recipients = COALESCE(@recipients+';' , '') + emailAddress from DJmailDetails_T where sendMail = 1 */
/*This sends the message*/
SET @mailMessage = 'Table update failure' --something apposite
SET @mailSubject = 'Error in ' + Isnull(@Procedure_Name,'Ad-hoc Query')
SET @sql = 'exec master.dbo.xp_sendmail ''' + @recipients + ''',@message = ''' + @mailMessage + ''',@query = ''select ''''' + @error_message + ''''' as ErrorMessage,' + Isnull(CAST@Error_status AS VARCHAR),' ') + ' as ErrorNumber'',@subject = ''' + @mailSubject + ''',@dbuse = ''master'',@attach_results = false,@no_output = true,@width = 8000'
PRINT (@sql) --change this from print to an exec to 'really' do it. --exec (@sql) --or uncomment this line
/* Script to drop the table when finished testing if object_id('DJmailDetails_T') > 0
drop table DJmailDetails_T
*/
I liked this so much I posted it on my site at http://glossopian.co.uk
Dave J
http://glossopian.co.uk/ "I don't know what I don't know."
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, July 30, 2012 1:17 PM
Points: 29,
Visits: 402
|
|
How would you add the subject to the message instead of getting the default of "SQL SERVER MESSAGE"? I've tried adding the @subject = 'Test Subject' but get an error "Must pass parameter number 2 and subsequent parameters as '@name = value'. After the form '@name = value' has been used, all subsequent parameters must be passed in the form '@name = value'." I then tried to add DECLARE @MYSUBJECT nvarchar (255) and then SET @MYSUBJECT = 'Test Subject' and then Exec Master.dbo.xp_sendmail @mysubject ,@MyRecipients, @MyMessage END END . That gives an ODBC error 170 (42000) Any help is appreciated.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, February 03, 2009 8:58 AM
Points: 1,
Visits: 2
|
|
My code is below, my issue is when this executes, lets say the query brings back 2 email address, instead of sending one to each name it send 2 to each name. How/where can I fix this?? CREATE PROCEDURE usp_email_ccb AS DECLARE @TITLE VARCHAR(500) DECLARE @DESCRIPTION VARCHAR(500) DECLARE @REQUIREMENTS VARCHAR(500) Declare @MyRecipients nvarchar (255) Declare @NEWMessage nvarchar (255) Declare @NEWSubject nvarchar (255) DECLARE @RECORD_NUM NCHAR(2) Declare MyCursor Cursor For Select LOGINID From USERS Where TASK='CCB' Open MyCursor Fetch Next From MyCursor Into @MyRecipients While @@Fetch_Status = 0 BEGIN Print @MyRecipients Fetch Next From MyCursor Into @MyRecipients select @record_num=[id], @title=[title], @description=[description], @REQUIREMENTS=[REQUIREMENTS] from display a,(select max(id) mxid from display) b where a.id=b.mxid SET @NEWSUBJECT='YOU HAVE AN IT REQUEST TO APPROVE!!! THIS IS A TEST EMAIL FOR THE WAP TOOL, JUST DISREGARD!!!' SET @NEWMESSAGE='TITLE: '+@TITLE + " " + +CHAR(13)+ +CHAR(13)+ 'DESCRIPTION: '+@DESCRIPTION + " " + +CHAR(13)++CHAR(13)+ 'REQUIREMENTS: '+@REQUIREMENTS + " "++CHAR(13)++CHAR(13)+ + 'Click on this link to view your request. http://localhost/WAP/db/ba2btpeb2.asp?a=GETRECORD&ID='+@RECORD_NUM Exec Master.dbo.xp_sendmail @MyRecipients, @SUBJECT =@NEWSUBJECT, @MESSAGE=@NEWMESSAGE
End Close MyCursor Deallocate MyCursor GO
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, January 07, 2009 12:23 AM
Points: 10,
Visits: 24
|
|
Hi david, Thanks for the good script. But after executing script, I got an error message. it is " xp_sendmail: Procedure expects parameter @user, which was not supplied. " help me to overcome from it. Thanking you.
mayur.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, January 07, 2009 12:23 AM
Points: 10,
Visits: 24
|
|
Hi, Thanks for the such a wonderful script. I run your script, its working fine, but mail haven't send to any one. I think There could be extra setting needs to do for the xp_sendmail.
Would you Please email, what are the Pre-requities to send a mail from Sql server 2005. my mail id is mayurs@winsoftech.com. I appreciate your time and help. Thanking you. regards, mayur
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Friday, March 08, 2013 11:15 AM
Points: 440,
Visits: 1,785
|
|
This is written for, and tested on SQL 2000 I'm afraid. I do not have 2005 so I can't help you there. I assume the xp_sendmail parameters have changed.
Or another thought, out of the box in SQL 2005 is xp_sendmail disabled? If so, have you enabled it?
HTH
Dave J
http://glossopian.co.uk/ "I don't know what I don't know."
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 1:56 PM
Points: 6,997,
Visits: 13,941
|
|
In 2005, xp_sendmail should be avoided. It's been replaced by sp_sendDBMail, which tends to operate much better (it's SMTP-based, so doesn't required an outlook client to be installed and configured; it also doesn't wait for the e-mail to complete, so your SQL server doesn't have to become dependent on where Exchange Server is up or down....) It's not on by default, so you need to use the Surface Area Config tool to enable it.
---------------------------------------------------------------------------------- Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 25, 2012 2:03 AM
Points: 9,
Visits: 55
|
|
Hi David;
I am using the same to send Mail from SQL Server 2000
But i am getting this error message: xp_sendmail: failed with mail error 0x80040111
Could you please help me out in this case , i need the solution
Thanks !!
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Friday, March 08, 2013 11:15 AM
Points: 440,
Visits: 1,785
|
|
This is a guess, but is Outlook installed on the server? And if so, is it configured correctly?
Dave
http://glossopian.co.uk/ "I don't know what I don't know."
|
|
|
|