I am trying to create a task in LINUX Ubuntu, where I make a query to my database and after that send an email with the extracted information, the problem is that I have searched several pages and I cannot make the task execute and send the mail, the code of the .SH file is as follows:
#!/bin/bash
$host = 'localhost';
$user = 'root';
$password = 'contraseña';
$db = 'mibasedatos';
$conection = @mysqli_connect($host,$user,$password,$db);
if(!$conection)
{
echo "Error en la conexión";
}
$consulta = mysqli_query($conection,"SELECT id_formulario, nombre_usuario, correo_usuario,
dd_mm_inicio FROM servicio WHERE id_formulario=7");
if ($resultado = $mysqli->query($consulta))
{
while ($fila = $resultado->fetch_assoc())
{
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.office365.com';
$mail->Username = 'mi correo';
$mail->Password = 'contraseña de mi correo';
$mail->isHTML(true);
$mail->setFrom('PRUEBA');
$mail->addAddress($correo_usuario);
$mail->Subject = 'inica el curso el dia: '.$fila["dd_mm_inicio"];
$mail->Body = 'inicia el curso!';
$mail->send();
$mail->clearAddresses();
$mail->clearAttachments();
}
$resultado->free();
In my system I can send mail but from PHP files but I don't know how to send mail from .SH file
You don't need a .sh file or searching how to make them.
First change everything to be how you would write a php file like you do on your web server, this is change the #!/bin/bash line and type <?php and save your file with the php extension.
To execute a php file in the Linux terminal, it can be done as follows:
For what I repeat, it is not necessary to create a shell file, you can also put the full path of the php executable, which is usually /usr/bin/php.
Now Linux has something we call crontab, which is the utility for scheduled tasks, commonly we would try to edit the scheduled tasks of the user we are logging in with with the command:
You have to change the word "user" for the user in which you are going to create the scheduled task. This command will open us in an editor, either vim or nano, a file with the information of the "user" crontab, in this you must include the information of the scheduled task that you want to create with parameters of when it should be executed and the execution terminal type, for example you could write:
That it would seek to execute the .php file on the first day of the month of December at 11:59 p.m., I proceed to explain what the field-by-field line means:
I could give you other examples:
That it would seek to execute daily at 9:15am, 12:15am, 3:15pm and 6:15pm the file.php and save the output in the file output.txt
That I would seek to execute the .php file daily every 30 minutes and save the output and possible error outputs in the output.txt file
If you don't know how to use vim, some basic commands that I can give you are that to enter the file edition you have to use the "i" key, to exit the edition mode you use the "escape" key, to save it can be executed ":x!" in escape mode.