F
F
Farik Azizov2021-07-06 18:41:03
PHP
Farik Azizov, 2021-07-06 18:41:03

How to attach the created file to the form and send it to the mail?

Hello everyone, please help, there is an html form with a textarea , if you write something in the textarea and press the SEND button, then a file with text from the textarea is created on the server , now how can I make it so that when the SEND button is pressed, the created file is sent to the mail ?

Form handler code:

<?php
    extract($_REQUEST);
    $file = fopen("form-data.txt","w+");
    fwrite($file,"Text :");
    fwrite($file, $textbox ."\n");
    fclose($file);
    header("location: index.php");
 ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2021-07-06
@bizpartneer

Use some lib to send mail messages, let's say PHPMailer
The docs have an example of sending a message with an attachment.

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
.....
    //Attachments
$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

PS Your code can be reduced to one line (I will clarify that it would be nice to add a check on the contents of the textarea)
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/filename.txt', $_REQUEST['textbox']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question