the best answer for this is even if you could, you shouldn't, it is a massive security risk to allow remote rewriting of your server files
as for why it is not safe lets say your file.php contains
$email = "defaultValue";
i then used your function to set the email to "; file_put_contents("file.php", ""); $junk = "
afterwards your file.php would read
$email = ""; file_put_contents("file.php", ""); $junk = "";
then when you ran file.php it would wipe itself
better ways to do the things you are describing are
parameters so in file.php
you have a function that accepts $email as a parameter and uses that
file inclusion
if you have this code
$email = "blah@foo.bar";
include('file.php');
where "file.php' is
<?php echo $email;?>
will output blah@foo.bar
you could also combine this with a config file
ie
include('config.php');
echo $email;
where "config.php' is
<?php $email = "blah@foo.bar";?>
Session if you set a session variable of Email then you can use that from any file that is on the same session
if(!isset($_SESSION))
{
session_start();
}
$email = "defaultValue"
if (array_key_exists("email", $_SESSION))
$email = $_SESSION["email"];
EDIT: to clarify the examples above as you are having trouble conceptualising
send an email via a parameter
lets say you have a PHP file as so
function SendEmail(array $email)
{
$transport = Swift_SmtpTransport::newInstance();
$transport->setLocalDomain();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo($email);
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("Test");
$mailer = Swift_Mailer::newInstance($transport);
return $mailer->send($message);
}
or using an externally defined variable
$email = "blah@foo.bar";
include('file.php');
where file.php is
$transport = Swift_SmtpTransport::newInstance();
$transport->setLocalDomain();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo($email);
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("Test");
$mailer = Swift_Mailer::newInstance($transport);
return $mailer->send($message);
$email = "blah@foo.bar";
include('file.php');
or using a session
function updateEmail($email) {
if(!isset($_SESSION))
{
session_start();
}
$_SESSION["email"] = $email;
}
updateEmail("test@gmail.com");
where file.php is
if(!isset($_SESSION))
{
session_start();
}
$email = "defaultValue"
if (array_key_exists("email", $_SESSION))
$email = $_SESSION["email"];
$transport = Swift_SmtpTransport::newInstance();
$transport->setLocalDomain();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo($email);
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("Test");
$mailer = Swift_Mailer::newInstance($transport);
return $mailer->send($message);
all of the above are safe ways to change a variable in a external code file with out opening your system to hacking