html form to edit php code - php

I'm working on a project that allows users to edit PHP code in PHP file.
I have a PHP page that uses the mail function to send emails. I want to allow the user to change the message sent out (in the mail function).
I know how to re-write over a file but how to edit it, I'm not sure...
So here's the mail function:
$name=$_POST['userName'];
$emaily=$_POST['userEmail'];
$email = "$sponsor_email";
$subject = "the subject";
$message = "This is the message I want the user to be able to edit.";
mail($email, $subject, $message, "From: $user-email");
Now I've got the $message var printed out on in the html form (so the users can see what will be changed but i don't know how I'd go about actually writing the code to change it on from submit.
Any ideas?
Thanks!
Sorry guys....
I didnt explain the situation properly.
The php file with the mail function will not be used to send an email from that form. All i want the form to do is edit the php file basically. I don't want to send the email with this form, this is for users to edit the mail function. Hope thats more clear...

On your editing page, just do something like below:
$message = file_get_contents("path to message file");
echo "<textarea id=\"message\">".$message."</textarea>";
Then you can edit whatever the contents of the file were.
Might be better though if you used a database, and sanitized the user input - rather than saving everything to a file.

Put the textarea in the form named "message", then do $message = $_POST['message'];

create a PHP that read what is in the PHP file that contains code to be edited then print it out in a HTML textarea then make another file that will write that modified code in the PHP file that has the code(to be modified). You can use google find how to read and write in a file.

Related

How to echo user input within mail body

I'm working on php mailer and I've tried all possible means to echo out the user input within the mail body, but i keep getting an error. below is my code
$mail->isHTML(true);
$body="<p><center><img src='https://example.com/images/mail-icon.png'><br>Example</center></p>
<center><h3>You are welcome</h3></center><br>
<p>
$mail->='$_POST['fname']'
Answer for your question is very simple. So, I dont know how your form looks, but all you need to do is:
$body=$fname;
Perhaps, inside $body you can write also html message, as $body="<p>Dear,<br> You just received a message from:". $fname ."</p>";

How can I use a PHP email variable with cron?

I have a service that sends out text to an email entered after x number of days. I want to use cron, but I know that because my PHP script uses variables, this would not work. How can I change my PHP or do anything that would allow me to use cron (or even something else)? I just need something where it will store the emails then send them. I'm really new to PHP, so keep it simple please.
Here’s my code:
<?php
if(isset($_POST['email']))
{
$headers = "From: Memory Jet <your_company#example.com>\r\n";
$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
mail($to_visitor, "Your Memory", $common_data, $headers);
}
?>
use argv array to read CLI params - http://php.net/manual/en/features.commandline.usage.php
if your script is named /bin/script.php then if invoked as /bin/script.php xyz the following:
$email = $argv[1];
will assign 'xyz' to $email.
just read doc I've provided - there's all you need
if you're wanting to store the data to transmit later, either using a database to store the info or writing it to a file would allow you to retrieve it.
If you need to potentially edit and manipulate the data later on, I'd recommend using a database.
I'd also recommend looking at http://www.tizag.com/phpT/ for some good, simple tutorials for PHP that really helped me when I first got going.

How do I use php so that when a user presses a specific button, they can write to a specific .txt file?

Basically, as in other questions I've asked related to my php chat application, I am trying to get it so that there is a text field where $msg is displayed via msg.txt. Two users can communicate to another in this way. This would be easy if I wanted to use a simple include function. But I don't want to take all the trouble to make and upload all those pages to my server. So how can I have it where when the user, say named Aaron, clicks on a button titled Benjamin, and types to a file called aaronbenjamin.txt, and if Aaron wants to talk to another user, he can press on a button titled Chris, and type to a file called aaronchris.txt? And all from the same box and text field? Thanks, I appreciate it.
EDIT: This is my code-
http://key-mail.zxq.net/msg.txt
Well, you should learn about using forms with PHP, with regards to your comment. If you read that tutorial fully, it should answer all of your questions about forms and php.
As far as getting it to write to a different text file, since you seem worried about "uploading all those pages", you'll be happy to know there's an easy solution!
There's a function called file_put_contents, which will create or write into a file.
Since you're new(ish) to PHP, here's an example:
<?php
$file = 'hello.txt';
$text = 'Hello World!';
file_put_contents($file, $text);
?>
This puts the contents of the $text variable into the file with the name stored in $file.
Reading from a file is similarly easy, with file_get_contents.
Assuming the file hello.txt exists from before and has the same contents, you can use the following code to read from the file and output its contents:
<?php
$file = 'hello.txt';
echo(file_get_contents($file));
?>
That will show the contents of $file.
Now, moving into the specifics of your question, if your form sets a "to" and a "from" GET variable where "to" is your username already, then the following code would write the value in a "message" GET variable into the file based on the pattern you gave:
<?php
$to = addslashes($_GET['to']);
$from = addslashes($_GET['from']);
$msg = addslashes($_GET['message']);
//addslashes is used as a small security measure
$file = $to . $from . '.txt';
file_put_contents($file, $msg);
?>
This fetches our variables from the GET array, sanitizes them to some extent, sets the file name, and writes the message to the file.
If you have any questions about specific parts of this or you'd like me to go into more detail, please feel free to ask!
This is how to write to a file:
http://php.net/manual/en/function.fopen.php
You're better off to use a database tho for sure.
To make different links do different things, you can use AJAX as you suggested or you can use GET variables to route functions on the PHP side. The latter is easier but means you will need to reload the page after the user presses the button.
here's a little demo:
click here
then at the top of the page in php:
if($_GET['clicked']==1){--write to file1---}
else if($_GET['clicked']==2){--write to file2---}
Hope it helps

Execute codes in a different page but remain on the same actual php page

I have a complicated problem here..I have codes to send mail using PEAR which i have tested in a php page called testmail.php.
Now i have my actual application an a page called Cart.php where i have a button called Place Order. When i click on this button, it actually redirects to a url called :
http://localhost/final/index.php?OrderSuccessful which actually displays a message on the page and sends an email. The problem is that when i put the code for email in Cart.php, i get errors. But when i put the url http://localhost/final/testmail.php it actually works.
So i was thinking, is there a way to execute the codes from that testmail.php by remaining on the page Cart.php?
include('Mail.php');
$mail = Mail::factory("mail");
$headers = array("From"=>"xyz#gmail.com", "Subject"=>"Your order has been placed ");
$body = "lol";
$mail->send("abc#live.com", $headers, $body);
I get the error
Assigning the return value of new by
reference is deprecated
Seems like you have old code. Follow the tips here:
ERRNO: 8192 when trying to send mail

php email html link, but just showing up as html code

I'm trying to e-mail a html table, that has links within it. But when I receive the e-mail, it just shows me the html code itself.
I'm using PHP pear to send the email.
I try constructing a string like so
$body = "<table>";
$body = $body . "<tr><td><a href='http://google.ca'>Google</a></td></tr>";
$body = $body . "</table>";
then e-mailing it, but when I receive the e-mail, it comes like this
<table><tr><td><a href='http://google.ca'>Google</a></td></tr></table>
Any suggestions? Thanks!
You'll want to ensure you are passing HTML to the setHTMLBody() function. If the problem continues, we'll need to see more of your PHP code.
It doesn't look like you're setting the Content or MIME-type for HTML email (which means its just being sent in plain text).
Check out this link for a guide on HTML email in PHP: http://articles.sitepoint.com/article/advanced-email-php

Categories