数据库更新时自动电子邮件到管理员 [英] Automatic E-Mail to Admin when Database is Updated

查看:259
本文介绍了数据库更新时自动电子邮件到管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法配置在MySQL数据库中添加新数据时发送的自动电子邮件。最好在PHP中。



想法是当新信息提交给数据库时,会自动生成包含新信息的电子邮件,并附上链接以查看在线信息并发送到预先配置的电子邮件地址。



目的是为了预约系统。同事们可以提交日期和休假时间,团队经理会在被添加或更改时自动获知。



我已尝试使用Word Press,但是太笨重了,比我需要的还要强大得多。我也在Stack Flow中注意了另外两个问题,但是他们没有回复或答案,并不是严格相同的问题。



任何帮助将不胜感激。 p>

解决方案

如果你有一个SMTP服务运行,你可以outfile到drop目录。如果您的音量很大,可能会导致重复的文件名,但有办法避免这种情况。



否则,您将需要创建一个UDF。 >

以下是一个示例触发器解决方案:

  CREATE TRIGGER test.autosendfromdrop BEFORE INSERT ON test.emaildrop 
FOR EACH ROW BEGIN
/ *开始撰写电子邮件文件* /
SELECT concat(To:,NEW.To),
concat( From:,NEW.From),
concat(Subject:,NEW.Subject),
NEW.Body
INTO OUTFILE
C:\\ inetpub\\mailroot\\pickup\\mail.txt
FIELDS TERMINATED由'\r\\\
'ESCAPED BY'';
END;

要标记邮件正文,您将需要这样的东西...

  CREATE FUNCTION`HTMLBody`(Msg varchar(8192))
RETURNS varchar(17408)CHARSET latin1 DETERMINISTIC
BEGIN
声明tmpMsg varchar(17408);
set tmpMsg = cast(concat(
'Date:',date_format(NOW(),'%​​e%b%Y%H:%i:%S -0600'),'\r \\\
',
'MIME-Version:1.0','\\\\
',
'Content-Type:multipart / alternative;','\\\\'
'boundary = \---- = _ NextPart_000_0000_01CA4B3F.8C263EE0\','\\\\
',
'Content-Class:urn:content-classes:message' '\r\\\
',
'重要性:正常','\\\\',
'优先级:正常','\\\\' '\\\\','','\\\\'',
'这是MIME格式的多部分消息。','\\\\' ,'\\\\
',
'------ = _ NextPart_000_0000_01CA4B3F.8C263EE0','\\\\
',
'Content-Type:text / plain;' ,'\\\\
',
'charset = \iso-8859-1 \,'\\\\',
'内容传输编码: 7位, '\r\\\
', '', '\r\\\
', '','\\\\
',
Msg,
'\r\\\
','','\r\\\
','','\r \\\
',
'------ = _ NextPart_000_0000_01CA4B3F.8C263EE0','\\\\
',
'Content-Type:text / html','\r\\ \\ n',
'Content-Transfer-Encoding:7bit','\r\\\
','','\r\\\
',
Msg,
' \r\\\
','------ = _ NextPart_000_0000_01CA4B3F.8C263EE0--'
)as char);
返回tmpMsg;
END;


Is there a way I can configure automatic e-mails to be sent when new data is added to a MySQL database. Preferably in PHP.

The idea is that when new information is submitted to the DB an e-mail is automatically generated containing the new information with a link to view the information online and sent to a pre-configured e-mail address.

The purpose of this is for a leave booking system. Colleagues will be able to submit dates and times of leave and the team manager is automatically informed as and when they are added or changed.

I have tried using Word Press but it is far too clunky and a lot more powerful than I need. I have also noted another couple of questions here on Stack Flow but they have no responses or answers and not strictly the same question.

Any help would be greatly appreciated.

解决方案

If you have an SMTP service running, you can outfile to the drop directory. If you have high volume, you may result with duplicate file names, but there are ways to avoid that.

Otherwise, you will need to create a UDF.

Here's a sample trigger solution:

 CREATE TRIGGER test.autosendfromdrop BEFORE INSERT ON test.emaildrop
 FOR EACH ROW BEGIN
  /* START THE WRITING OF THE EMAIL FILE HERE*/      
  SELECT  concat("To: ",NEW.To),
          concat("From: ",NEW.From),
          concat("Subject: ",NEW.Subject),
          NEW.Body
      INTO OUTFILE 
               "C:\\inetpub\\mailroot\\pickup\\mail.txt" 
          FIELDS TERMINATED by '\r\n' ESCAPED BY '';            
    END;

To markup the message body you will need something like this...

CREATE FUNCTION `HTMLBody`(Msg varchar(8192)) 
RETURNS varchar(17408) CHARSET latin1 DETERMINISTIC
BEGIN
declare tmpMsg varchar(17408);
set tmpMsg = cast(concat(
  'Date: ',date_format(NOW(),'%e %b %Y %H:%i:%S -0600'),'\r\n',
  'MIME-Version: 1.0','\r\n',
  'Content-Type: multipart/alternative;','\r\n',
  ' boundary=\"----=_NextPart_000_0000_01CA4B3F.8C263EE0\"','\r\n',
  'Content-Class: urn:content-classes:message','\r\n',
  'Importance: normal','\r\n',
  'Priority: normal','\r\n','','\r\n','','\r\n',
  'This is a multi-part message in MIME format.','\r\n','','\r\n',
  '------=_NextPart_000_0000_01CA4B3F.8C263EE0','\r\n',
  'Content-Type: text/plain;','\r\n',
  '  charset=\"iso-8859-1\"','\r\n',
  'Content-Transfer-Encoding: 7bit','\r\n','','\r\n','','\r\n',
  Msg,
  '\r\n','','\r\n','','\r\n',
  '------=_NextPart_000_0000_01CA4B3F.8C263EE0','\r\n',
  'Content-Type: text/html','\r\n',
  'Content-Transfer-Encoding: 7bit','\r\n','','\r\n',
  Msg,
  '\r\n','------=_NextPart_000_0000_01CA4B3F.8C263EE0--'
  ) as char);
 RETURN tmpMsg;
 END ;

这篇关于数据库更新时自动电子邮件到管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆