Skip to content

Instantly share code, notes, and snippets.

@niklasravnsborg
Forked from richartkeil/easymail.php
Last active August 29, 2015 14:12
Show Gist options
  • Save niklasravnsborg/a9d6ae02bc06c2a792d0 to your computer and use it in GitHub Desktop.
Save niklasravnsborg/a9d6ae02bc06c2a792d0 to your computer and use it in GitHub Desktop.
Send Mails with PHP
<?php
// USAGE
// include easymail.php `include('easymail.php');`
// Use like this `sendMail('to which mail address',
// 'message',
// 'subject',
// 'from which mail address',
// 'name to',
// 'name from');`
//
// The only necessary argument is the first one all the others are optional.
function validMail($mail) {
if (filter_var($mail, FILTER_VALIDATE_EMAIL) === false) {
return 0;
} else {
return 1;
}
}
function sendMail($mailTo, $text = '', $subject = '', $mailFrom = '', $nameTo = '', $nameFrom = '') {
$mailTo = (string) $mailTo;
$mailFrom = (string) $mailFrom;
$text = (string) $text;
$subject = (string) $subject;
$nameTo = (string) $nameTo;
$nameFrom = (string) $nameFrom;
// if $mailTo is not valid or $mailFrom is not empty and not valid
if (!validMail($mailTo) || (!empty($mailFrom) && !validMail($mailFrom))) {
return 0;
}
$to = $nameTo . ' <' . $mailTo . '>';
$from = $nameFrom . ' <' . $mailFrom . '>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
return mail($to, $subject, $text, $headers);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment