I'm trying to send an automated e-mail through PHP, that prints some pre-defined variables. It works as expected if I write the php script directly into the main file (akkoord.php). However, I would like to save the script as a function in a separate php file (email.php), because I want to include it in other php files as well. I saved the script as email.php, turned it into a function mail() and included email.php in akkoord.php. The function works and it sends an email, but the variables (that I defined in akkoord.php) are not coming through. I figured this would be a scope problem, so I redefined them as global variables in email.php. However, it still doesn't work.
(simplified) Akkoord.php:
<html>
<head>
<?php
$naam = $_GET["naam"];
$reisnaam = $_GET["reisnaam"];
?>
<?php
include'email.php';
[some other code that works fine]
email();
?>
</head>
<body>
[some body]
</body>
</html>
And email.php:
global $reisnaam;
function email(){
global $reisnaam;
global $naam;
$to = 'my_email@email.my';
$subject = $reisnaam ;
$message = 'Hello'.$naam;
$headers = 'From: some@body.nn';
if(mail($to, $subject, $message, $headers))
echo "Mail sent";
else
$fout .= "Not sent";
}
?>
I checked if email.php is included - it was (after all, it does send the email). Idem for the function email(). Any ideas of where I'm going wrong?
*edit: fixed the ' in $headers