Skip to content

Instantly share code, notes, and snippets.

@lordmatt
Created February 7, 2025 21:00
Show Gist options
  • Save lordmatt/547319f0ca4d8231c1837cafaef73dd7 to your computer and use it in GitHub Desktop.
Save lordmatt/547319f0ca4d8231c1837cafaef73dd7 to your computer and use it in GitHub Desktop.
A purely educational example of loops, text amnipulation, and mail sending
<?php
/*
* This code is for illustration purposes only. I can in not endorse running this
* on a cron job or anything else annoying like that. Seriouesely, dont.
*
* If you install WAMP/LAMP to run this locally be good and sure that you
* redirect your local DNS to null to avoid accidently spamming the example
* addresses.
*
* As this snippet is purely to demonstrate PHP loops and email methods, I
* release it into the public domain. Please use this for testing only and not
* to protest Trump and Musk or aything chaotic like that. Behave yourself.
*
* For how to config sendmail -> https://www.php.net/manual/en/function.mail.php
*
* FYI, this script will take a long time to run. See if you can figure out why.
*
* Also, I challenge you to write a better version (optionally in another
* language if you are feel adventurous)
*
*/
// change this to an addrezss your server is authorised to send from
$from_address = '[email protected]';
// do not use these addresses to test this script because that might disrupt the
// work that DOGE is doing and that would be a big old crying shame.
$pure_example_addresses = array('[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]');
// some text to play with. Change as you see fit.
$some_example_text = <<<EOF
We're no strangers to love
You know the rules and so do I
A full commitment's what I'm thinkin' of
You wouldn't get this from any other guy
I just wanna tell you how I'm feeling
Gotta make you understand
--
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
--
We've known each other for so long
Your heart's been aching, but you're too shy to say it
Inside, we both know what's been going on
We know the game and we're gonna play it
And if you ask me how I'm feeling
Don't tell me you're too blind to see
--
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
--
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
--
We've known each other for so long
Your heart's been aching, but you're too shy to say it
Inside, we both know what's been going on
We know the game and we're gonna play it
I just wanna tell you how I'm feeling
Gotta make you understand
--
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
--
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
--
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
--
Is this the right address for renewing my driving license?
Bob from church said this was the one
--
Testing, testing
Is this thing on?
--
Remember this Friday is dress down Friday
George will be bringing cake. Henry is doing a ranch dip. Please let Linda know what foods you will bring for breaktime.
EOF;
$to = implode(', ', $pure_example_addresses); // this turns the array into a string
/*
* The side effect of putting many emails into the "to" field can be an email
* storm when everyone starts hitting "reply all".
*/
// this is how you set variouse headers
$additional_headers = 'From: ' . $from_address . "\r\n" .
'Reply-To: [email protected]' . "\r\n" ; // this is where the replies should go please
$content = explode('--', $some_example_text);
// now we have an array of text things
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Output of the general tutorial on PHP loops and email</title>
</head>
<?php flush(); ?>
<body>
<h1>Output of the general tutorial on PHP loops and email</h1>
<pre>
<?php
// time to do something with each of them...
set_time_limit(1000);
foreach($content as $one_bit){
// now we want a subject.
// to get that we will split by line break
// for more on that see -> https://stackoverflow.com/a/11165332/1499072
$lines = preg_split("/\r\n|\n|\r/", trim($one_bit));
$subject = $lines[0]; // first one as subject
unset($lines); // we are done with that now
$message = $one_bit;
// anything else you want to add each time
#$message .= ''; ## The .= means add the text (note the dot)
# echo "[{$subject}][{$one_bit}]=====\r\r\r"; ## if you want to debug
set_time_limit(1000); // so we have all the time we need
flush(); // send some bytes
sleep(rand(1,12)); // wait a random ammount of seconds
if(
mail(
$to,
$subject,
$message,
$additional_headers,
''
)
){
echo "Mail sent to [{$to}] with subject [{$subject}]";
}else{
echo "Mail FAILED to [{$to}]";
}
echo "<hr />\n\r\n\r";
flush(); // send some bytes
}
?></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment