0

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

AmLam
  • 43
  • 6
  • 2
    Is there a reason that you're using a global variable instead of just passing it to the function as a parameter? – Josh Feb 21 '20 at 20:25
  • 1
    Putting `global` outside the function accomplishes nothing. – Barmar Feb 21 '20 at 20:28
  • You also have a parse error in this `$headers = 'From: some@body.nn;`. If what you posted isn't "that", then please.... do post your real code. – Funk Forty Niner Feb 21 '20 at 20:47
  • `$fout .= "Not sent";` < that too without declaring an empty variable (wiithout the dot) will throw a parse error. – Funk Forty Niner Feb 21 '20 at 20:48
  • I tried without 'global', and with 'global' in different places even if it didn't make sense. I thought it should even work without 'global', as I expected the variables in akkoord.php to be global by default. I'm not passing it as a parameter, because I want to add more variables that should appear somewhere in the email, and I thought this method would be easier (but come to think of it; it might not...). $fout is declared in akkoord.php (but I forgot to include it here, because the rest of akkoord.php is irrelevant to this problem). – AmLam Feb 21 '20 at 21:05

1 Answers1

1

Thanks Josh, for the subtle hint. I passed the variables as parameters, and now it does work as expected:

<?php
 function email($reisnaam, $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";
   }
?>

and in akkoord.php i changed email() to email($reisnaam,$naam).

I must admit that I do not exactly understand why the first method did not work, but I'm happy this one does. Thanks!

AmLam
  • 43
  • 6