Jump to content
hariskar

How can a form after validated be sent with SMTP?

Recommended Posts

I followed the lessons about php and php forms. Everything is clear and very useful, but I would like to ask how could I send eg this form if valid with SMTP? So, I would like to first validate the form and then send it with SMTP.

 

Thank you!

Edited by hariskar

Share this post


Link to post
Share on other sites

You can send mail with the PHP mail() function.

 

If you don't want to do that you'll need to do complicated socket connections with an external SMTP server. There are some mail libraries out there that do that for you, I think one of them is called PHPMailer.

  • Like 1

Share this post


Link to post
Share on other sites

Once all the fields have been validated, assign to variables and use these variables as parameters within mail() function as shown in mail tutorial.

  • Like 1

Share this post


Link to post
Share on other sites

From a test server that uses (I believe) sendmail I can send the form to my email with

<?php
$msg = "$name $subject $email $comment";
$msg = wordwrap($msg,70);
mail("my_email@xxxx.com","My subject",$msg);
?>

But in my real server I can send only with smtp, in th email tutorial I can not see any instructions how to put in the smtp parameters in mail().

 

Thank you!

 

Edit: As I see mail() does not support smtp, so I have to use PHPMailer.

Edited by hariskar

Share this post


Link to post
Share on other sites

If I put

action="email.php"

where I have put all PHPMailer parameters I can send my form with email (smtp).

But now I have

action="<?php echohtmlspecialchars($_SERVER["PHP_SELF"]);?>

, so how can I make my form be sent with email.php?

 

Thank you!

Share this post


Link to post
Share on other sites

I presume it is sent to itself for validation, once validated, set the post values as session values, then use header redirect to 'email.php', read session values and use for phpmailer() parameters send email and header redirect to for example a thank you page.

Share this post


Link to post
Share on other sites

Thank you for reply, but it seems difficult for me to do what you say (although I read the php tutorials)...

I will continue reading..

Edited by hariskar

Share this post


Link to post
Share on other sites

I don't know why you sent it back to form page in the first place, because you have not provided any code? If it can be sent to email.php with all validation processing done there then send it to email.php. I presumed that it had php validation of input fields when submitted. So i can't give a definite answer as depending on how you want it to be processed determines the final solution on sending email through smtp, without it! it can take several different paths to the final solution.

Share this post


Link to post
Share on other sites

You are right, let's get more specific: I followed the php form tutorial and made this form:

 

<?php
// define variables and set to empty values
$emailErr = $radio1Err = $radio2Err = $radio3Err = $radio4Err = $radio5Err = $dateErr= "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["email"])) {
    $email = "";
  } else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Λάθος μορφή email"; 
    }
}
  if (empty($_POST["date"])) {
    $dateErr = "Παρακαλώ επιλέξτε την ημερομηνία!";
  } else {
    $date = test_input($_POST["date"]);
  }
  if (empty($_POST["radio1"])) {
    $radio1Err = "Παρακαλώ επιλέξτε";
  } else {
    $radio1 = test_input($_POST["radio1"]);
  }
  if (empty($_POST["radio2"])) {
    $radio2Err = "Παρακαλώ επιλέξτε";
  } else {
    $radio2 = test_input($_POST["radio2"]);
  }
  if (empty($_POST["radio3"])) {
    $radio3Err = "Παρακαλώ επιλέξτε";
  } else {
    $radio3 = test_input($_POST["radio3"]);
  }
  if (empty($_POST["radio4"])) {
    $radio4Err = "Παρακαλώ επιλέξτε";
  } else {
    $radio4 = test_input($_POST["radio4"]);
  }
  if (empty($_POST["radio5"])) {
    $radio5Err = "Παρακαλώ επιλέξτε";
  } else {
    $radio5 = test_input($_POST["radio5"]);
  }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<form autocomplete="on" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset style="border:solid 1px #c1c1c1;border-radius:5px">
<legend>Ατομικά στοιχεία (προαιρετικά):</legend>
Ονοματεπώνυμο:<br>
<input type="text" name="name" size="40" style="font-size:15px"><br>
Επάγγελμα:<br>
<input type="text" name="job" size="40" style="font-size:15px"><br>
Τηλέφωνο:<br>
<input type="text" name="phone" size="40" style="font-size:15px"><br>
Διεύθυνση:<br>
<input type="text" name="address" size="40" style="font-size:15px"><br>
email:<br>
<input type="text" name="email" size="40" style="font-size:15px">
<span class="error"><?php echo $emailErr;?></span>
</fieldset>
Σημερινή ημερομηνία:<br>
<input type="date" name="date">
<span class="error">* <?php echo $dateErr;?></span>
<fieldset style="border:solid 1px #c1c1c1;border-radius:5px">
<legend>Πως θα αξιολογούσατε:</legend>
<h4>1. Την ποιότητα των υπηρεσιών μας</h4>
<input type="radio" name="radio1" value="1"> 1
<input type="radio" name="radio1" value="2"> 2
<input type="radio" name="radio1" value="3"> 3
<input type="radio" name="radio1" value="4"> 4
<input type="radio" name="radio1" value="5"> 5
<span class="error">* <?php echo $radio1Err;?></span>
<h4>2. Την ταχύτητα παράδοσης των αποτελεσμάτων</h4>
<input type="radio" name="radio2" value="1"> 1
<input type="radio" name="radio2" value="2"> 2
<input type="radio" name="radio2" value="3"> 3
<input type="radio" name="radio2" value="4"> 4
<input type="radio" name="radio2" value="5"> 5
<span class="error">* <?php echo $radio2Err;?></span>
<h4>3.  Την ποιότητα της εξυπηρέτησης και τη συνεργασιμότητα των υπάλληλων του εργαστηρίου μας</h4>
<input type="radio" name="radio3" value="1"> 1
<input type="radio" name="radio3" value="2"> 2
<input type="radio" name="radio3" value="3"> 3
<input type="radio" name="radio3" value="4"> 4
<input type="radio" name="radio3" value="5"> 5
<span class="error">* <?php echo $radio3Err;?></span>
<h4>4. Τις εγκαταστάσεις και τους χώρους του εργαστηρίου μας</h4>
<input type="radio" name="radio4" value="1"> 1
<input type="radio" name="radio4" value="2"> 2
<input type="radio" name="radio4" value="3"> 3
<input type="radio" name="radio4" value="4"> 4
<input type="radio" name="radio4" value="5"> 5
<span class="error">* <?php echo $radio4Err;?></span>
<h4>5. Την καθαριότητα του εργαστηρίου μας</h4>
<input type="radio" name="radio5" value="1"> 1
<input type="radio" name="radio5" value="2"> 2
<input type="radio" name="radio5" value="3"> 3
<input type="radio" name="radio5" value="4"> 4
<input type="radio" name="radio5" value="5"> 5
<span class="error">* <?php echo $radio5Err;?></span>
</fieldset>
<h4>Σχόλια που θα θέλατε να κάνετε ή προτάσεις για βελτίωση:</h4>
<textarea style="resize:none;" name="message" rows="10" cols="80" style="font-size:15px;">
</textarea>
<input type="submit" name="submit" value="Υποβολή" style="margin-top:20px"> 
</form>

I want it if all required fields are filled to be sent to my email, so I want on submit to check if everything is OK and then open email.php to send it to me with PHPMailer. email.php will be something like this (I have not tried it yet because I try to figure out the "action" issue):

 

<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require("contact-test2/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port= 587;  
$mail->Username = "xx@xx.xx";
$mail->Password = "xxx";
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->SetFrom("xx@xx.xx","xx.xx");
$mail->AddAddress("xx@xx.xx", "xx.xx");
$mail->AddReplyTo($_POST["email"], $_POST["name"]);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = "<html>
<p>$_POST["name"]<br>$_POST["email"]<br>$_POST["date"]<br>$_POST["radio1"] ..etc..</p>
</html>";

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Σας ευχαριστούμε για την επικοινωνία!";
?>

Thank you!

Share this post


Link to post
Share on other sites

I did it!

Here is what I did (it needs some content corrections):

 

<?php
$nameErr = $emailErr =  $subjectErr = $messageErr = "";
$name = $email =  $subject = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = true;
   if (empty($_POST["name"])) {
    $nameErr = "Name is required";
$valid = false;
  } else {
    $name = test_input($_POST["name"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
$valid = false;
  } else {
    $email = test_input($_POST["email"]);
  }
 if (empty($_POST["subject"])) {
    $subjectErr = "Name is required";
$valid = false;
  } else {
    $subject = test_input($_POST["subject"]);
  }
  if (empty($_POST["message"])) {
    $messageErr = "Email is required";
$valid = false;
  } else {
    $message = test_input($_POST["message"]);
  }
 if($valid){
   header('Location: email.php');
   exit();
  }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

I added

$valid = false;

and

 if($valid){
   header('Location: email.php');
   exit();
  }

Is it a problem that I have some common variables and functions in 2 forms they are in 2 different pages?

$nameErr, $emailErr, $name, $email, $message

and function test_input($data) ?

 

Thank you for all help!

Share this post


Link to post
Share on other sites

New problem, no way form input passes to emal.php..

I tried to put in form.php

<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['name'] = $_POST['name'];
}
?>

and in email.php

$name=$_SESSION['name'];

or

$email = $_REQUEST['email'] ;

without success..

Share this post


Link to post
Share on other sites

1) Did you have session_start(); at the very top of both pages where sessions is used before any html code OR php?

2) IF you are going to pass input value to session would it not be better to pass the sanitized validated values BEFORE redirecting to email.php, rather than values directly from $_POST or $_REQUEST.

3) You don't have $_POST with name 'Submit', only with name 'submit', they are treated as different names.

Edited by dsonesuk
  • Like 1

Share this post


Link to post
Share on other sites

There is no reason to have 2 different PHP files. The validation and email code belong in the same file, they should both be executed. It's not a good idea to have a PHP script sitting there which sends an email without validating anything, and there's no reason to split the code up into multiple files. Instead of redirecting if things are valid, just actually send the email.

  • Like 1

Share this post


Link to post
Share on other sites

First of all thanks a lot for your help and patience!

What I did till now is make the form work with 2 files and import all input to the 2nd file except the file attachment, I can not make it pass to email.php till now.

I added

<?php
session_start();
$_SESSION = $_POST;
?>

in 1st file

and

<?php
session_start();
?>

in the 2nd

and the 2 files are:

 

<?php
$nameErr = $emailErr =  $subjectErr = $messageErr = "";
$name = $email =  $subject = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = true;
   if (empty($_POST["name"])) {
    $nameErr = "Name is required";
$valid = false;
  } else {
    $name = test_input($_POST["name"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
$valid = false;
  } else {
    $email = test_input($_POST["email"]);
  }
 if (empty($_POST["subject"])) {
    $subjectErr = "Name is required";
$valid = false;
  } else {
    $subject = test_input($_POST["subject"]);
  }
  if (empty($_POST["message"])) {
    $messageErr = "Email is required";
$valid = false;
  } else {
    $message = test_input($_POST["message"]);
  }
 if($valid){
   header('Location: email.php');
   exit();
  }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<form id="contact" name="contact" autocomplete="on" style="margin-left:9px" method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Το όνομά σας<input id="name" name="name" type="text"><span class="error"><?php echo $nameErr;?></span><br>
Το email σας<input id="email" name="email" type="text"><span class="error"><?php echo $emailErr;?></span><br>
Θέμα<input id="subject" name="subject" type="text"><span class="error"><?php echo $sunjectErr;?></span><br>
Μήνυμα<br >
<textarea id="message" name="message" rows="6" cols="60"></textarea><span class="error"><?php echo $messageErr;?></span><br>
Βιογραφικό<input id="cv" type ="file" name='attachment'>
<input id="submit" type="submit" value="Υποβολή" style="margin-top:10px">
</form>

and

 

<?php
require("contact-test2/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port= 587;  
$mail->Username = "xx@xx.xx";
$mail->Password = "xxx";
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->SetFrom("xx@xx.xx","xx.xx");
$mail->AddAddress("xx@xx.xx", "xx.xx");
$mail->AddReplyTo($_SESSION["email"], $_SESSION["name"]);
$mail->WordWrap = 80;
$mail->IsHTML(true);
$mail->Subject = $_SESSION["subject"];
$mail->Body =  "Όνομα: " . $_SESSION['name'] ."<br>Email: " . $_SESSION['email'] . "<br><br>Μύνημα<br>" . $_SESSION['message'];
$mail->AltBody = $message;
$mail->addAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name']);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Σας ευχαριστούμε για την επικοινωνία!";
?>

So now I will try to combine these 2 files. So I will remove sessions, keep

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>

put

if($valid){
 $mail->Send();
  }

and where/how will I put if sent

header('Location: thank-you.php');

Thank you!

Share this post


Link to post
Share on other sites

Right, add the code for sending the validated information where you check if everything is valid, you can add the attachment also. You can redirect to a thank you page after sending the email.

Share this post


Link to post
Share on other sites

This is the best that I have achieved: Now I have 1 php instead of 2, all inputs + file attachment goes to email. But 2 things are missing:

if($valid){
   ......
   exit();
  }

What should I put there instead of

header('Location: email.php');

(now there is no validation, all mails come, even totally empty).

 

and the redirect to thankyou.php.

If I put

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

"Message sent!" is above the form before I even put the inputs, if I put

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: thankyou.php');
}

I when I open form.php it is immediately redirected to thankyou.php (form.php does not show at all). So I left else, thank you and redirection out at the moment.

 

<?php
$nameErr = $emailErr =  $subjectErr = $messageErr = "";
$name = $email =  $subject = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = true;
   if (empty($_POST["name"])) {
    $nameErr = "Name is required";
$valid = false;
  } else {
    $name = test_input($_POST["name"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
$valid = false;
  } else {
    $email = test_input($_POST["email"]);
  }
 if (empty($_POST["subject"])) {
    $subjectErr = "Name is required";
$valid = false;
  } else {
    $subject = test_input($_POST["subject"]);
  }
  if (empty($_POST["message"])) {
    $messageErr = "Email is required";
$valid = false;
  } else {
    $message = test_input($_POST["message"]);
  }
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
require("contact-test2/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port= 587;  
$mail->Username = "xx@xx.xx";
$mail->Password = "xxx";
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->SetFrom("xx@xx.gr","xx.xx");
$mail->AddAddress("xx@xx.xx", "xx.xx");
$mail->AddReplyTo($_POST["email"], $_POST["name"]);
$mail->WordWrap = 80;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body =  "Όνομα: " . $_POST['name'] ."<br>Email: " . $_POST['email'] . "<br><br>Μύνημα<br>" . $_POST['message'];
$mail->AltBody = $message;
$mail->addAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name']);
if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
?>
<form id="contact" name="contact" autocomplete="on" style="margin-left:9px" method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Το όνομά σας<input id="name" name="name" type="text"><span class="error"><?php echo $nameErr;?></span><br>
Το email σας<input id="email" name="email" type="text"><span class="error"><?php echo $emailErr;?></span><br>
Θέμα<input id="subject" name="subject" type="text"><span class="error"><?php echo $subjectErr;?></span><br>
Μήνυμα<br >
<textarea id="message" name="message" rows="6" cols="60"></textarea><span class="error"><?php echo $messageErr;?></span><br>
Βιογραφικό<input id="cv" type ="file" name='attachment'>
<input id="submit" type="submit" value="Υποβολή" style="margin-top:10px">
</form>
Edited by hariskar

Share this post


Link to post
Share on other sites

All of the code to process and handle the form should go inside the if statement where you check if the request method is post. Your code will try to send the email regardless of the request method, you need to move the code inside the if statement. You should also only send the email if everything is valid. You check if everything was valid, but then you don't do anything with $valid before sending the email.

 

if (form was submitted) {
  validate form

  if (form is valid) {
    send email
  }
}
You have this:

 

if (form was submitted) {
  validate form
}
send email
  • Like 1

Share this post


Link to post
Share on other sites

And finally this is the php where everything works!

Thank you very much!!!

:D

 

<?php
$nameErr = $emailErr =  $subjectErr = $messageErr = "";
$name = $email =  $subject = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = true;
   if (empty($_POST["name"])) {
    $nameErr = "Name is required";
$valid = false;
  } else {
    $name = test_input($_POST["name"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
$valid = false;
  } else {
    $email = test_input($_POST["email"]);
  }
 if (empty($_POST["subject"])) {
    $subjectErr = "Name is required";
$valid = false;
  } else {
    $subject = test_input($_POST["subject"]);
  }
  if (empty($_POST["message"])) {
    $messageErr = "Email is required";
$valid = false;
  } else {
    $message = test_input($_POST["message"]);
  }
require("contact-test2/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port= 587;  
$mail->Username = "info@xxx.gr";
$mail->Password = "xxx";
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->SetFrom("info@xxx.xx","xxx.xx");
$mail->AddAddress("xx@xxx.xx", "xxx.xx");
$mail->AddReplyTo($_POST["email"], $_POST["name"]);
$mail->WordWrap = 80;
$mail->IsHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body =  "Όνομα: " . $_POST['name'] ."<br>Email: " . $_POST['email'] . "<br><br>Μύνημα<br>" . $_POST['message'];
$mail->AltBody = $message;
$mail->addAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name']);
if($valid){
if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
} else {
header('Location: thank-you');}}
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
Edited by hariskar

Share this post


Link to post
Share on other sites

By using $_POST['...'] in email you possibly allowing injection of code into email, you have already ran these in a function to prevent this and assigned the values to variables so why not use these? $name, $email, $message etc.

 

You need to check this out for more update validating and sanitizing methods to use in your function test_input()

http://www.w3schools.com/php/php_filter.asp

Share this post


Link to post
Share on other sites

These were easy!

Thank you again!

I added some kind of uploaded_file validation, but I am not feeling very secure with it because as I see it is based on the file extension.

 

<?php
$nameErr = $emailErr =  $subjectErr = $messageErr = fileErr= "";
$name = $email =  $subject = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = true;
   if (empty($_POST["name"])) {
    $nameErr = "Παρακαλώ συμπληρώστε το όνομα";
$valid = false;
  } else {
    $name = test_input($_POST["name"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Παρακαλώ συμπληρώστε το email";
$valid = false;
  } else {
  $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Μη έγκυρη μορφή email"; 
$valid = false;
    }
  }
 if (empty($_POST["subject"])) {
    $subjectErr = "Παρακαλώ συμπληρώστε το θέμα";
$valid = false;
  } else {
    $subject = test_input($_POST["subject"]);
  }
  if (empty($_POST["message"])) {
    $messageErr = "Υποχρεωτικό πεδίο";
$valid = false;
  } else {
    $message = test_input($_POST["message"]);
  }
function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }
    return $ip;
}
$user_ip = getUserIP();
require("contact/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port= 587;  
$mail->Username = "xxx@xxx.xx";
$mail->Password = "xxx";
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->SetFrom("xxx@xxx.xx","Φόρμα επικοινωνίας");
$mail->AddAddress("xxx@xxx.xx", "xxx.xx");
$mail->AddReplyTo($email, $name);
$mail->WordWrap = 80;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = "Όνομα: $name<br>Email: $email <br>IP: $user_ip<br><br>Μήνυμα<br>$message";
$mail->AltBody = $message;
$mail->addAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name']);
if($_FILES['attachment']["type"] != "application/pdf" &&
if($_FILES['attachment']["type"] != "application/pdf" &&
$_FILES['attachment']["type"] != "application/msword") { 
      $fileErr = "Μη έγκυρη μορφή file"; 
$valid = false;
}
if($valid){
if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
} else {
header('Location: thank-you');}}
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
Edited by hariskar

Share this post


Link to post
Share on other sites

There are several file upload errors that you should check for that PHP will tell you about, the $_FILES array has an "error" index that PHP uses for error codes.

 

http://php.net/manual/en/features.file-upload.php

http://php.net/manual/en/features.file-upload.errors.php

  • Like 1

Share this post


Link to post
Share on other sites

Thanks I will check that.

 

Input values in the form are lost when form submitted with errors and the user has to re-type all input. How could that be avoided?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...