简体   繁体   English

Linux服务器:向我的用户发送邮件

[英]Linux server: Send mail to my users

I am fairly new to creating server scripts and jobs that the server runs every day. 对于创建服务器每天运行的服务器脚本和作业,我还很陌生。

My problem is as follow: 我的问题如下:

I want to send an email to my users reminding them of a specific job they have to do. 我想向用户发送电子邮件,以提醒他们他们必须完成的特定工作。

My idea: 我的点子:

Database -> collect all users who needs to be notified and insert them into a table notify_user 数据库->收集需要通知的所有用户,并将其插入到表notify_user

Script -> find all users and send them a mail 脚本->查找所有用户并向他们发送邮件

Script -> Delete all from the table 脚本->从表中删除所有

This script will then run at a specific time every day for instance every 24th hour. 然后,该脚本将在每天的特定时间(例如每24小时)运行。

As I stated earlier I am not really keen on how to setup such a script. 如前所述,我并不十分热衷于如何设置这样的脚本。

My server is an Ubuntu server and my application is a PHP program. 我的服务器是Ubuntu服务器,我的应用程序是PHP程序。

Does anyone know how I might achieve this or know where I might find some documentation on this subject since I have been unable to find anything that solves this issue. 有谁知道我将如何实现这一目标,或者知道在哪里可以找到有关该主题的文档,因为我一直无法找到解决该问题的方法。

If given that you know how to populate the 'notify_user' table then these are my steps to reproduce a solution sample for you. 如果您知道如何填充“ notify_user”表,那么这些是我为您重现解决方案示例的步骤。 I did this on my VPS server with sendmail daemon running. 我在运行sendmail守护程序的VPS服务器上执行了此操作。

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 167
Server version: 5.5.40-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  create database stack_mail_db;
Query OK, 1 row affec`enter code here`ted (0.05 sec)
mysql> grant all privileges on stack_mail_db.* to 'stack_mail_usr'@'localhost' identified by 'stack_mail_pass';
Query OK, 0 rows affected (0.11 sec)
mysql> use stack_mail_db;
Database changed
mysql> create table notify_user( id int not null auto_increment primary key, user_name tinytext, user_email tinytext );
Query OK, 0 rows affected (0.28 sec)

After creating this sample database we should populate it with at least 2 users (for testing) with working emails. 创建此示例数据库后,我们应该至少有2个用户(用于测试)使用有效的电子邮件填充该数据库。 I changed here the actual emails I used. 我在这里更改了我实际使用的电子邮件。

mysql> insert notify_user (user_name, user_email) values ('test1', 'test1@test.com');
Query OK, 1 row affected (0.18 sec)

mysql> insert notify_user (user_name, user_email) values ('test2', 'test2@test.net');
Query OK, 1 row affected (0.03 sec)

Now we should write a script that gets these details and sends emails: 现在,我们应该编写一个脚本来获取这些详细信息并发送电子邮件:

# vim cron_email.php
<?php
$host = 'localhost';
$user = 'stack_mail_usr';
$pass = 'stack_mail_pass';
$dbname = 'stack_mail_db';

$conn = new mysqli($host, $user, $pass, $dbname);

if ($conn->connect_error) {
        trigger_error('DB connection failed: ' . $conn->connect_error, E_USER_ERROR);
}

$query = 'select * from notify_user';

$res = $conn->query($query);

if ($res === false) {
        trigger_error('Failed query: ' . $query . ' Error: ' . $conn->error, E_USER_ERROR);
}

$headers = 'From: admin@example.com' . "\r\n" .
        'Reply-To: admin@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
        $to = $row['user_email'];
        $subject = 'Notification for ' . $row['user_name'];
        $message = 'Hello ' . $row['user_name'];
        $mail = mail($to, $subject, $message, $headers);
        if ($mail) {
                $conn->query('delete from notify_user where id=' . $row['id']);
        } else {
                echo "Email failed\n";
        }
}

Now it's time to put this script on cron: 现在是时候将该脚本放到cron上了:

# crontab -e
0 0 * * * php -f /path/to/cron_email.php

This will run your script exactly every midnight. 这将完全在每个午夜运行脚本。 If you want to set a more specific hour, look at this tutorial: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ 如果要设置更具体的时间,请查看本教程: http : //www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses /

Hope this helps ^) 希望这会有所帮助^)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当MX是另一台服务器时如何从我的Linux服务器将php mail()发送到user@domain.com - How send php mail() to user@domain.com from my linux server when mx is another server 如何让PHP向本地用户发送邮件(cakePHP,Linux SLES) - How to get PHP to send mail to local users (cakePHP, Linux SLES) 如何为我的网站的多个用户发送一封邮件? - How to send single mail for multiple users of my website? 服务器站点邮件未发送 - Server Site Mail Not Send 在我的服务器上查找初始化“发送邮件”任务的脚本/进程 - Find Script/Process initializing a “Send Mail” task on my Server 我可以使用PHPMailer从邮件服务器发送邮件吗? - Can i use PHPMailer to send from my mail server? PHP:php mail(),如何在我自己的Linux服务器上设置没有smtp的php邮件功能? - PHP: php mail(), how to setup php mail function without smtp on my own linux server? 在Linux上,我想停止对特定Windows服务器的身份验证,该服务器将使用该服务器通过Sendmail发送邮件 - On Linux I want to stop authentication for a specific Windows server that will use it to send mail using Sendmail 基于PHP和MYSQL向多个用户发送邮件 - Send mail to multiple users based on PHP and MYSQL 使用php发送邮件给多个用户 - send mail to multiple users using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM