This will send a file by email:
$htmlFile = "/path/myFile.html";
$fd = fopen ($htmlFile, "r");
$message = fread ($fd, filesize ($htmlFile));
fclose ($fd);
mail($emailAddress, $subject, $message);
If you want to send a php file, you can't use the local path to access the file. If you do it will not evaluate the php code, it will just send the file the way it is on your computer.
But if you use $htmlFile = "http://localhost/myFile.php" the php output of myFile.php will be sent by email.
myFile.php could be a email template that looks like this:
Hello <?= $userName ?>
....
Then you could use a script like below to send email to an list of people.
// let's say you have an array $people of objects that each have
// a userName and email property
foreach ($people as $person){
$htmlFile = "/path/http://localhost/myFile.php?userName=" . $person['userName'];
$fd = fopen ($htmlFile, "r");
$message = fread ($fd, filesize ($htmlFile));
fclose ($fd);
mail($person['email'], "News for " . $person['userName'], $message);
}
obviously you can pass more variables to the myFile.php by adding &var2=val2&var3=val3 etc...