-2

this is a project to school but i dont know why its not working... I have the input fields , the first one is for my email , the second one is for my colleague email, then the subject and the body.... but when i fill the inputs it doesnt send the email....

This is my html...

<html>
<body>
    <h2>Script done by Nuno Fernandes and Rafael</h2>
    <form action="mail.php">
        E-mail do sender:<br>
        <input type="text" name="From"><br>
        E-mail do receiver:<br>
        <input type="text" name="addAddress"><br>
        Assunto:<br>
        <input type="text" name="Subject"><br>
        Corpo do texto:<br>
        <input type="text" name="Body" size="100"><br><br>
        <input type="submit" value="Send">
    </form>
 </body>
 </html>

and this is my php (that is called mail.php)

 <?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//Cria o objeto
$mail = new PHPMailer(true);

//Dados de quem envia
$mail->From = $_POST['From'];

//Dados de quem vai receber
$mail->addAddress=$_POST['addAddress'];

//Se quiseres que a opção de reply funcione, metes aqui o email de reply
$mail->addReplyTo=$_POST['addReplyTo'];

//CC e BCC
//$mail->addCC("cc@example.com");
//$mail->addBCC("bcc@example.com");

//Isto determina se envias como texto normal ou se tens HTML a formatar o mail
$mail->isHTML(true);

$mail->Subject = $_POST['Subject']; // Assunto

$mail->Body = $_POST['Body']; // Mensagem

//$mail->AltBody = ""; // Isto é no caso de quereres ter html em cima e no fim do email uma versao em texto normal

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
  • 1
    It seems as though you are not processing the data sent from the form – tola Mar 08 '22 at 16:54
  • 1
    Its going where you sent it, but you sent it NOWHERE `$mail->addAddress(""); ` – RiggsFolly Mar 08 '22 at 16:58
  • @tola , how can i make it to process the data from the form to the php? – Nunofernandes.official Mar 08 '22 at 17:01
  • @RiggsFolly, im not understanding sorry , because i send the input for example, like you said to $mail -> addAddress and i created that parament on the form. i supossed that when i enter on the form it would send to addAddress btw sorry for my english – Nunofernandes.official Mar 08 '22 at 17:02
  • 1
    You need to either listen better in class or just spend a little time with [a simple tutorial](https://www.tutorialrepublic.com/php-tutorial/php-form-handling.php) – RiggsFolly Mar 08 '22 at 17:08
  • 1
    HTML basics: Your form needs a method if you are going to use POST. `
    ` Once you make that change, the $_POST should be filled.
    – gview Mar 08 '22 at 18:10

1 Answers1

0

The values that are sent from the form will be in the $_POST array.

So to set the message, it would look like:

$mail->Body = $_POST['Body']; // Mensagem

The ['Body'] part of that will match the name attribute from your HTML form.

However, using a form like this to send emails from your server is wide open to attack by spammers. If you don't add some security, spammers can use this to send any kind of email to anyone they want, which will likely get your account banned from your hosting provider. Please look into securing your mailer forms or use one that someone else has already made. Here's an article that could get you started

Stevish
  • 734
  • 5
  • 17
  • Thanks for your help, i will try to change my code to that, and about security thats not a problem, because its just for a university project. we are using it on localhost using apache and after the work is presentated we gonna close this... – Nunofernandes.official Mar 08 '22 at 17:05
  • btw i changed the php, but i think it still not working... – Nunofernandes.official Mar 08 '22 at 17:12
  • 1
    See my comment about your lack of a method in your form tag. – gview Mar 08 '22 at 18:11
  • Oh, sorry, my bad. I misremembered and thought POST was the default method when it's really GET. If you change all the `$_POST` variables to `$_GET` OR add `method="post"` to your form tag (not both), then it should work. – Stevish Mar 08 '22 at 18:13