16c03-2.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 96 行
PHP
96 行
<?php// Prepare by setting a timezone, mail() uses this.date_default_timezone_set('America/New_York');// A function that takes an array of email addresses, and a message to send.// and connects to a SMTP server, also specified, to deliver the content.function mass_email($emails, $from, $subj, $message, $smtp, $moreheaders = '') { // First order of business is to connect to the SMTP server - port 25: if (!($sp = stream_socket_client("tcp://{$smtp}:25", $err_num, $err_string))) { exit("ERROR: Failed to connect - {$err_num} - {$err_string}\n"); } // Read the response, we should be welcomed with a 220 response _check_response(fgets($sp), '220'); // Now, be friendly and say hello .. Note we are using the server // specified name ... If this is run from the command line, this will // not exist and will need set specifically in some other fashion: $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'example.com'; fputs($sp, "HELO {$host}\n"); // And make sure that they claim this was ok (250 response) _check_response(fgets($sp), '250'); // Now we can start looping, sending an email to each ... foreach ($emails as $e) { // Allow ourselves 10 seconds to complete this one email: set_time_limit(20); // Say who the email is from: fputs($sp, "MAIL FROM: {$from}\n"); _check_response(fgets($sp), '250'); // Now who is the email to: fputs($sp, "RCPT TO: {$e}\n"); // In this case, a 'bad' response is ok. It means they don't accept // that particular email address as valid, so skip, and move on: if (_check_response(fgets($sp), '250', false)) { // Ok, we made it this far. Prepare for the body fputs($sp, "DATA\n"); _check_response(fgets($sp), '354'); // Send headers, include the 'moreheaders' parameter if provided fputs($sp, "To: {$e}\nFrom: {$from}\nSubject: " . "{$subj}\n{$moreheaders}\n\n"); // Now we can send the body: fputs($sp, $message); // And our custom footer fputs($sp, "\n\n-- Sent to: {$e} by the " . "PHP 5 in Practice Mass Mailer\n"); // Now close the data with a single period on a line: fputs($sp, ".\n"); _check_response(fgets($sp), '250'); } else { // If we got here the recipient was bad. // So reset our state to start fresh fputs($sp, "RSET\n"); _check_response(fgets($sp), '250'); } } // Close the connection, we are done! fputs($sp, "QUIT\n"); fclose($sp); // Before exiting, reset the time limit to its original value: set_time_limit(ini_get('max_execution_time'));}// A small utility function that we will use to check the response from the// server: It checks response against the expected response. If bail is// set to true, it was a BAD error and exits out. Otherwise it just// returns false.function _check_response($response, $expected, $bail = true) { // Compare the response to what was expected: if (strncmp($response, $expected, strlen($expected)) != 0) { // If we were told to bail, do so: if ($bail) { exit("ERROR: Bad Response\nExpected: {$expected}Received: {$response}"); } else { // Just return false so this can be handled return false; } } // If we made it here, return true return true;}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?