/** * Creates a new message by reading from a file containing a complete mail * message. The headers are parsed and stored; the body is left intact. * * \param $filename * The filename of the file to read * * \return * A new AnewtMailMessage instance */ public static function from_file($filename) { assert('is_string($filename)'); $data = file_get_contents($filename); $message = AnewtMailMessage::from_string($data); return $message; }
#!/usr/bin/php -q <?php require_once '../anewt.lib.php'; anewt_include('mail'); $m = new AnewtMailMessage(); function usage() { printf("Usage: %s address\n", $_SERVER['argv'][0]); exit(1); } $address = array_get_default($_SERVER['argv'], 1, null); if (is_null($address)) { echo 'Error: no address specified', NL; usage(); } $m->add_header('To', $address); $m->add_header('Subject', 'Test'); $m->add_header('From', $address); $m->set('body', 'This is a test message sent using the AnewtMailMessage class.'); $result = $m->send(); if ($result) { echo 'Mail sent successfully.', NL; } else { echo 'Sending mail failed.', NL; }
printf("Usage: %s [filename] omit filename or specify - to use stdin\n", $_SERVER['argv'][0]); } /* Parse arguments */ $argv = $_SERVER['argv']; array_shift($argv); // drop argv[0] $argc = count($argv); $read_stdin = false; if ($argc == 0) { usage(); die; } if ($argc == 1 && $argv[0] == '-') { $read_stdin = true; } /* Read stdin */ if ($read_stdin) { $fp = fopen('php://stdin', 'r'); $message = AnewtMailMessage::from_stream($fp); fclose($fp); /* Read from file */ } else { $filename = $argv[0]; $message = AnewtMailMessage::from_file($filename); } echo $message->has_header('From') ? '"From" header found!' : 'No "From" header found!'; echo NL; echo $message->get_header('From'), "\n"; echo $message->get_header('To'), "\n"; echo $message->get_header('Subject'), "\n"; echo str_truncate($message->get('body'), 500), "\n";