Send email with MySQL information

1

As the question says I need to send an email with information obtained when executing a sentence ( Select * from for example) but I have no idea how to do this (my MySQL level is not very advanced), in MSSQL I would do it by a job and using the mail client that comes in MSSQL by default, my question is how would I do something like this but with MySQL? I would appreciate any suggestions or comments since as I mentioned I have no idea.

PD. I know how I will create the job (Event in MySQL) and how I will do the sentence, which is not like sending the email automatically without the user moving a single finger.

    
asked by Francisco Fernandez 20.09.2016 в 18:05
source

2 answers

1

With a UDF 1 it is possible to run an external program in MySQL. For example, you can install 2 the library SYS , which contains the sys_exec .

This function can be invoked in a trigger 3 and you can pass the arguments that are required to execute the external program.

CREATE TRIGGER trigger_name
AFTER INSERT
   ON table_name FOR EACH ROW

BEGIN

   -- variable declarations
   DECLARE exec_str VARCHAR(500);
   DECLARE ret_val INT;

   -- trigger code
   SET exec_str = CONCAT("sh /tmp/load.sh ", NEW.user_mail);
   SET ret_val = sys_exec(exec_str);
   IF ret_val = 0 THEN
      -- OK
   ELSE
      -- ERROR
   END IF;

END;

Notes

  • See §26.4 Adding New Functions to MySQL from MySQL 5.7 Reference Manual . There is a list of UDFs in http://www.mysqludf.org/ .
  • Some libraries offer an installation script. More details at §26.4.2.5 UDF Compiling and Installing from MySQL 5.7 Reference Manual . [Here] you can find a way.
  • See a tutorial on Create Trigger in MySQL . Also Example, create and use triggers in MySQL - YouTube .
  • answered by 20.09.2016 в 20:19
    0

    After reading your last comment, what I suggest is that you work with the cron.php .

    This file, is a "scheduled task", that you put the code'PHP', which you say you already have ready. Then at the server level, it's a matter of setting the time you want it to be sent.

    As a tip, first create the page and test it, to see what it does to you and then, you create the cron.

        
    answered by 20.09.2016 в 19:40