SET NOCOUNT ON;
USE [master];

DECLARE @SQLAgentEnabled            INT    = 0,
        @SQLAgentStarted            INT    = 0,
        @DBMailEnabled              INT    = 0,
        @MailProfileEnabled         INT    = 0,
        @MailAccountEnabled         INT    = 0,
        @SQLAgentMailEnabled        INT    = 0,
        @SQLAgentMailProfileEnabled sysname,
        @failed_email_error         INT    = 0;

-- SQL Server Agent enabled
SELECT @SQLAgentEnabled = CAST(value_in_use AS INT)
  FROM sys.configurations
 WHERE [name] = 'Agent XPs';

-- SQL Server Agent status
IF (SELECT CAST(SERVERPROPERTY('Edition') AS VARCHAR(30))) NOT LIKE 'Express Edition%'
BEGIN
    SELECT @SQLAgentStarted = CASE
                                   WHEN status_desc = 'Running' THEN 1
                                   ELSE 0 END
      FROM sys.dm_server_services
     WHERE servicename LIKE 'SQL Server Agent%';
END;

-- SQL Database Mail is enabled
SELECT @DBMailEnabled = CAST(value_in_use AS INT)
  FROM sys.configurations
 WHERE [name] = 'Database Mail XPs';

-- @SQLAgentMailEnabled
SELECT @MailProfileEnabled = CASE
                                  WHEN COUNT(*) > 0 THEN 1
                                  ELSE 0 END
  FROM msdb.dbo.sysmail_profile;

-- @MailAccountEnabled
SELECT @MailAccountEnabled = CASE
                                  WHEN COUNT(*) > 0 THEN 1
                                  ELSE 0 END
  FROM msdb.dbo.sysmail_account;

-- SQL Server Agent is enabled to use Database Mail
EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
                                       N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
                                       N'UseDatabaseMail',
                                       @SQLAgentMailEnabled OUTPUT;

-- SQL Server Agent is enabled to use Database Mail and Mail Profile is assigned
EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
                                       N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
                                       N'DatabaseMailProfile',
                                       @SQLAgentMailProfileEnabled OUTPUT;

-- Check if there are failed email send in the last 7 days
SELECT @failed_email_error = CASE
                                  WHEN COUNT(*) > 0 THEN 1
                                  ELSE 0 END
  FROM msdb.dbo.sysmail_event_log
 WHERE event_type = 'error'
   AND log_date   > DATEADD(DAY, -7, GETDATE());

-- Final report
SELECT @@SERVERNAME AS Server_Name,
       CAST(GETDATE() AS SMALLDATETIME) AS Run_Date,
       MailsAreSent = @SQLAgentEnabled * @SQLAgentStarted * @DBMailEnabled * @MailProfileEnabled * @MailAccountEnabled
                     * @SQLAgentMailEnabled * (CASE
                                                    WHEN @SQLAgentMailProfileEnabled IS NOT NULL THEN 1
                                                    ELSE 0 END) * (CASE
                                                                        WHEN ISNULL(@failed_email_error, 0) = 0 THEN 1
                                                                        ELSE 0 END),
       Notes = CASE
                    WHEN CAST(SERVERPROPERTY('Edition') AS VARCHAR(30)) LIKE 'Express Edition%' THEN
                        'Express Edition, DB Mail not supported'
                    ELSE
                        CASE
                             WHEN @SQLAgentEnabled = 0 THEN 'SQL Agent disabled; '
                             ELSE '' END + CASE
                                                WHEN @DBMailEnabled = 0 THEN 'DB Mail disabled; '
                                                ELSE '' END
                        + CASE
                               WHEN @MailProfileEnabled = 0 THEN 'Mail Profile disabled; '
                               ELSE '' END + CASE
                                                  WHEN @MailAccountEnabled = 0 THEN 'Mail Account disabled; '
                                                  ELSE '' END
                        + CASE
                               WHEN @SQLAgentMailEnabled = 0 THEN 'SQL Agent Mail disabled; '
                               ELSE '' END + CASE
                                                  WHEN @SQLAgentMailProfileEnabled IS NOT NULL THEN ''
                                                  ELSE 'SQL Agent Mail Profile disabled; ' END
                        + CASE
                               WHEN @failed_email_error > 0 THEN 'failed email(s) during last 7 days; '
                               ELSE '' END 
					END
                               