Last active
August 29, 2015 14:08
-
-
Save dduleone/07a0b51a85080aae95e5 to your computer and use it in GitHub Desktop.
Script to quickly test PHP mail().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * mailtest.php | |
| * | |
| * Provides a <s>simple</s> wrapper for testing PHP's mail() function. | |
| * Relies on sendmail | |
| * | |
| * @Author Dan DuLeone <dan.duleone@nbcuni.com> | |
| * | |
| * Required: | |
| * -i|--id Server Identifier | |
| * | |
| * Optional: | |
| * -t|--to To Address | |
| * -f|--from From Address | |
| * -s|--subject Message Subject | |
| */ | |
| /** | |
| * Usage: | |
| * | |
| * php mailtest.php --to "to@address.com" --from "from@address.com" --subject "Message Subject" --id "Server ID" | |
| * php mailtest.php -t "to@address.com" -f "from@address.com" -s "Message Subject" -i "Server ID" | |
| * | |
| * /opt/php-5.3.22/bin/php mailtest.php -i "Machine Name" | |
| */ | |
| $shortopts = "t:f:s:i:"; | |
| $longopts = array( | |
| "to:", | |
| "from:", | |
| "subject:", | |
| "id:" | |
| ); | |
| $opts = getopt($shortopts, $longopts); | |
| $to = ""; | |
| $subject = ""; | |
| $from = ""; | |
| $message = ""; | |
| if (isset($opts['id'])) { | |
| $id = $opts['id']; | |
| } elseif (isset($opts['i'])) { | |
| $id = $opts['i']; | |
| } else { | |
| die("Machine identifier is required. [-i|--id]\n"); | |
| } | |
| if (isset($opts['to'])) { | |
| $to = $opts['to']; | |
| } elseif (isset($opts['t'])) { | |
| $to = $opts['t']; | |
| } else { | |
| $to = "dan.duleone@nbcuni.com"; | |
| echo "No To Address provided. [-t|--to] Using default.\n"; | |
| echo "\tDefault: " . $to . "\n\n"; | |
| } | |
| if (isset($opts['from'])) { | |
| $from = $opts['from']; | |
| } elseif (isset($opts['f'])) { | |
| $from = $opts['f']; | |
| } else { | |
| $from = "emailtest@nbcuni.com"; | |
| echo "No From Address provided. [-f|--from] Using default.\n"; | |
| echo "\tDefault: " . $from . "\n\n"; | |
| } | |
| if (isset($opts['subject'])) { | |
| $subject = $opts['subject']; | |
| } elseif (isset($opts['s'])) { | |
| $subject = $opts['s']; | |
| } else { | |
| $subject = "[" .$id . "] PHP Mail() Test"; | |
| echo "No Message Subject provided. [-s|--subject] Using default.\n"; | |
| echo "\tDefault: " . $subject . "\n\n"; | |
| } | |
| $message = ""; | |
| $message .= "Testing mail sent from: " . $id . "\r\n"; | |
| $message .= "Sent: " . date('r',time()) . "\r\n"; | |
| $headers = array( | |
| "From: " . $from, | |
| "Reply-To: " . $from, | |
| "X-Mailer: PHP/" . phpversion() | |
| ); | |
| if(mail($to, $subject, $message, implode("\r\n", $headers))) { | |
| die("Mail sent successfully.\n"); | |
| } else { | |
| die("An error occurred. Mail was not sent.\n"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment