server | mail server
E-mail occupies a very important place in the network. At present, the mail server running in the enterprise use their own independent management system to achieve account creation, password modification, mailbox expansion, delete mailboxes and other functions. When the mailbox user reaches a certain number, the day-to-day maintenance of the mail administrator is extremely onerous, which requires that some of the administrative tasks (such as account application) be automated, and that a subset of the tasks (such as password modification) be held by multiple group administrators, freeing the mail manager from tedious maintenance work, Really to the mail server to play a regulatory role.
The choice of mail server and development environment
At present, many mail servers do not provide development interface, so in the enterprise to realize the development of mail management system, the first problem is to find a language can be combined with the development of open mail system.
Because of the high stability requirements, large e-mail systems generally use UNIX as the operating system for the server. For example, Hotmail uses the FreeBSD and Solaris systems.
Because of the defects in the system structure, the Linux-brought mails are difficult to withstand the large user's access. QMail is the GNU's next known free software, a new generation of UNIX messaging systems that support Maildir storage. It saves each message as a separate file in the user's personal mail directory. QMail supports both virtual domain and virtual user. The current domestic popular free e-mail system mostly uses QMail as the basic server software, provides the multi-level directory to support the large user number.
From a development point of view, Php+linux's development model has been adopted by many developers. The use of PHP to invoke the qmail provided by the control program, to achieve a variety of mail system management work. In order to realize the effective control to a large number of users, database management can also be introduced. This article will introduce an example of a PHP +oracle+qmail+linux development.
Overall design
The whole system is divided into two parts: mailbox management and Mail transceiver. When requesting a mailbox, fill in the necessary information, and the system will automatically create mailboxes and allow users to use them immediately. When users forget their passwords, they can use the current common practice of the Internet, by answering user-defined questions to modify the mailbox password. For security reasons, after the user answers the wrong question three times, the system locks the account's "Fix mailbox password" feature for 30 minutes.
Message management divides user rights into the following 3 levels according to different requirements:
Ordinary users can send and receive e-mail in the browser, modify the mailbox password, modify custom questions and answers, and query the mailbox.
In addition to the function of ordinary users, the group administrator can modify the password of all the accounts in this group, and remove the "Fix mailbox password" function lock of this section account.
The mailbox administrator can modify all the mailbox password, delete the mailbox, modify the mailbox capacity, view the application situation of the mailbox, remove the "Fix mailbox password" function lock of all account.
Send and receive mail with the standard POP3 function provided by PHP, you can get the mailing list, view the details of the message, send a message with multiple attachments, and provide Delete, reply and other functions.
The database consists mainly of the following tables:
Email_info Store All e-mail information, such as account number, person's name, department, contact telephone, custom questions, answers to custom questions, lock tags, etc.
Email_register_info information that is filled in when a user applies for an e-mail message, such as the account number, password, time of application, department, and the current step of the application (primarily to prevent users from maliciously skipping certain steps).
Email_change_log records the changes in the user's email address for future inspection. Mainly record the application time of the mail, delete time, mailbox account number, the person name of the mailbox operation, IP address and other information.
Technical difficulties analysis
PHP invokes the QMail control program
In a Linux environment, each program has a corresponding user and group concept. When the PHP program executes in the server, the default user is nobody and has no right to execute the QMail control program. There is a sudo command in Linux that allows other users to execute commands as root. Modify the/etc/sudoers file to add the following sections:
PHP Source:
--------------------------------------------------------------------------------
Nobody All=/var/vpopmail/bin/vadduser (add user)
Nobody All=/var/vpopmail/bin/vdeluser (delete user)
Nobody all=/var/vpopmail/bin/vpasswd (change password)
Nobody ALL=/VAR/VPOPMAIL/BIN/VCHKPW (check that the user password is correct)
Nobody All=/var/vpopmail/bin/vsetuserquota (modify mailbox Maximum capacity)
Nobody All=/var/vpopmail/bin/vmoduser (modify user information)
Root all= (All)
--------------------------------------------------------------------------------
When the QMail control program is invoked, the user is required to enter part of the information manually. If you want to automate, you will also need to use the redirection technology in Linux to deposit user input into a file and use redirection technology to pass it as a shell script.
PHP provides the system (), EXEC () function for executing external commands. The system () function executes the given command, output, and return results. The exec () function is similar to system () but does not output the results. The EXEC () function must be selected using program control, and depending on the state of execution to determine whether the controller executes correctly, and if there is an error, it needs to be advertised to the mail administrator. The following code describes the "Add user" feature as an example:
PHP Source:
--------------------------------------------------------------------------------
Require ("/home/httpd/phplib/qmail.inc");
$qmail _date = "V1";
$qmail _email = new Register_email;
$sql = "Select Pw_name from". $qmail _date;
$qmail _email->query ($sql);
while ($qmail _email->next_record ())
{
$qmail _user = $qmail _email->f ("Pw_name");
$passwd = "12345";
$str _command = "Sudo/var/vpopmail/bin/vadduser". $qmail _user.;
$str _command = $str _command. " @mail. com ". $passwd."-Q 10000000/home/n ";
@exec ($str _command, $str,& $result);
if ($result!= 0)
{
echo "command string is:". $str _command. ", the command is failed!!! <br> ";
$error _info = "failed while executing vadduser command";
$address = "from:computer@mail.com\nreply-to:computer@mail.com\n";
Mail ("webmaster@mail.com", $error _info, $message, $address);
}
}
$qmail _email->free ();
--------------------------------------------------------------------------------
Other functions such as modify password, modify mailbox capacity, delete mailboxes, etc., in the implementation of the program is very similar, do not do too much elaboration.
Upload and download attachments in e-mail
PHP itself provides the upload function, but according to the actual situation, you can do some configuration and error handling accordingly. Users can increase the maximum size of uploaded files by modifying the php.ini file. At the same time, you can limit the size of a single attachment to 5MB by adding a method to the form of a Web page. If the network speed is slow, you need to set the timeout variable for the page, otherwise the upload will time out because it is too long. Part of the code on the server side of the handler is as follows:
PHP Source:
--------------------------------------------------------------------------------
if (!file_exists ($mail _att))//To determine whether the file was uploaded successfully
{//error handling and display
break;
}
Copy ($mail _att, $mail _att. " Att "); Rename the attachment and copy it to the specified location.
for ($i = 1; $i <= $num _attach; $i + +)
{
$file _name = "Send_att". $i; Get the original name of the uploaded file
$file _tmp = "send_att_tmp". $i; Get the temporary file name of the uploaded file in the server
$file _size = "send_att_size". $i; Get the size of the uploaded file
$file _mime = "Send_att_mime". $i; Get the type of upload file
}
--------------------------------------------------------------------------------
When providing attachment downloads, the common practice is not used here, which is to provide the user with a URL to the file. Because this approach is theoretically unsafe, it is possible for users to download the file directly from the link. Conversely, a similar security issue is not encountered when a file is read from a directory that the browser cannot access and is provided to the user for downloading. The program core code is as follows:
PHP Source:
--------------------------------------------------------------------------------
if (file_exists ($attach _filepos))//Determine if the file exists
{
$size = filesize ($attach _filepos); Get the size of the file
The user's original file name is provided to the user for downloading.
Header ("content-disposition:attachment; Filename= ". $attach _filename);
Header ("Content-length:". $size);
Header ("content-type:application/x-zip-compressed");
ReadFile ($attach _filepos); read out the download file and provide it to the user for download.
}
--------------------------------------------------------------------------------
Conclusion
The system is technically perfect to combine PHP with Oracle database to realize the management of qmail users and the maintenance of daily mailbox server. At present, the system has been used within my company for more than a year, the effect is very good. It eases the day-to-day maintenance of mail administrators and provides online mail for users who are unfamiliar with the mail configuration. This system also has the authentication, the group management and the Mail sends off, the encoding and so on aspect function, because the space is limited does not elaborate.