-
-
Save niklasravnsborg/a9d6ae02bc06c2a792d0 to your computer and use it in GitHub Desktop.
Send Mails with PHP
This file contains 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 | |
// 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