PHP email form sends Array instead of variables - php

I'm trying to figure out a basic web email form. I got the code from somewhere and am trying to make the checkboxes options.
The email gets sent but just has Array next to the option.
What am I doing wrong?

Some sample code will help diagnose the problem.
At a guess I'd say you've got something like
$message = "contact from message\r\n";
$message .= $_POST['check'];
since $_POST['check'] is an array, you need to use it like:
$message .= implode(', ', $_POST['check']);
This will convert the array of selected services options into a comma delimited string.

If you are a PHP beginner, you can use PHP Forms builder that allows to create professionally looking email forms within just a few minutes. You will only need to make a PHP form by means of an intuitive interface and generate a code that can be easily copied and pasted to any web page.

Related

Is there a way to get the body of a Laravel Mail object to store in a variable?

I'm using the Laravel Mail API to generate mailables and send them out.
Everything is working fine with that.
However, I also want to log the email bodies in the DB.
In order to do that though, I need to first get the bodies of the emails I'm sending out.
In the Laravel docs, they have a snippet about how to output an email to the browser here:
https://laravel.com/docs/5.7/mail#previewing-mailables-in-the-browser
However, I don't want to output the email to the browser. I want to store it in a variable.
Is that possible? If so, how? Thanks.
Like the docs suggested, I tried something like the following:
$body = new App\Mail\InvoicePaid($invoice);
However, I got an error saying that I have to actually return the new Mail object in order for it to work (i.e., do what they do in the example code in the docs).
Previous paragraph tells you how to capture the rendered template:
$html = (new App\Mail\InvoicePaid($invoice))->render();
echo $html;

How do I turn a form into a attachable file for email in php

So I Have a form that will send out emails that correspond to how they filled the form. I have to take some information that was filled in the form and put it in a HTML form to send in a email which is done in php.
My question is, is there anyway to turn that form into a attachable file that could just be added to the mail function and sent instead?
Thank you for your time!
Yes, you can transform your HTML form into a... HTML page (using HTML tokens manually, or maybe something like TCPDF?), and save it to a temporary disk file, then attach it to the outgoing email. Of course, your mail function must support attachments! (If it doesn't, use a different mailer functions - there's many of them).
You can do it quickly by preparing the form manually and replacing the text fields with something like {{ Name }}.
Then just run a str_replace on the file_get_contents() of your template and save the results in a temporary file:
$translate = array(
'{{ Name }}' => $Form['Name'],
....
);
// one could also use preg_replace with 'e' flag and use directly $Form...
$html = str_replace(array_keys($translate), array_values($translate),
file_get_contents("form-template.html"));
In just the same way you can prepare a Word document, save it as RTF (which is ASCII readable), and attach the file with .doc extension -- most word processors will open it automatically.
If you want to send a PDF file (using TCPDF, FPDF, etc.), it's a bit more complicated but not too much.
Swift Mailer integrates into any web app written in PHP 5, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features including attachments.

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.

html form to edit php code

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.

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

Categories