0

I have seen this is a common question but through searching I haven't found the answer I need. I have used PHP code to create a mailer with attachments that are located on my server.

My site has a CRUD system and the read.php file displays various elements of an uploaded file. I also have a form on this page which allows the user to enter and email address and message to send on the said file.

My form code is:-

<form class="form-horizontal" action="mailer.php" method="post">

                  <div class="form-group">
                    <label class="col-sm-2">Email Address</label>
                    <div class="col-sm-4">
                        <input name="doc_email" type="text" placeholder="Enter Email Address to" class="form-control" >
                    </div>
                  </div>


                  <div class="form-group">
                    <label class="col-sm-2">Message</label>
                    <div class="col-sm-4">
                        <input name="doc_message" type="text"  placeholder="Enter a message (not mandatory)" class="form-control"  >
                    </div>
                  </div>



                  <div class="form-actions">

                      <button type="submit" class="btn btn-success">Send Email</button>
                                            </div>
                </form>

And my mailer.php code is currently;-

<?php 
if (isset($_POST['submit']))
    {
$file_name  = $_POST['file_name'];
$doc_email  = $_POST['doc_email'];
$doc_message    = $_POST['doc_message'];

require_once('class.phpmailer.php');

$email = new PHPMailer();
$email->From        = 'you@example.com';
$email->FromName    = 'Your Name';
$email->Subject     = 'Message Subject';
$email->Body        = $doc_message;
$email->AddAddress  = $doc_email; 

$path_file_to_attach     = '/documents/uploads/';
$file_to_attach      = $file_name;

$email->AddAttachment( $path_file_to_attach , $file_to_attach );

return $email->Send();

//redirect user after success
header("Location: index.php");
}
?>

The class.phpmailer.php file is within the same directory (/documents) as the read.php/mailer.php, all I get is a blank screen with no email or errors.

The folder with the files is located in a sub folder called 'uploads'... documents/uploads/

Any help as always is greatly appreciated.

Dan
  • 45
  • 1
  • 9
  • What error do you get? – Buurman Jul 28 '15 at 08:32
  • @Buurman I dont get any errors, just a blank screen – Dan Jul 28 '15 at 08:33
  • Okay, so disregard all the post stuff, start with hardcoding the email-address to one of yours and sending an email without attachment. Do you receive it? Is it maybe in your spam folder? If you do receive it, try it with a hardcoded attachment (path hardcoded, not the actual file contents). Does that also work? If yes, then the email sending itself works, so something else goes wrong. In essence, try to narrow down what goes wrong. Without a clearly defined error or problem it is very hard for anyone here to say what's wrong. – Buurman Jul 28 '15 at 08:38
  • @Buurman Thanks for your input I will try this method – Dan Jul 28 '15 at 08:39
  • Enable error output in PHP; that's the usual reason for a blank page. – Synchro Jul 28 '15 at 16:13

1 Answers1

0

You have two things empty:

 if (isset($_POST['submit'])) //this is false, your button must have a name="submit" attribute
 $file_name  = $_POST['file_name']; // you don't have any element with this name.

Just add to your html:

<input type="text" name="file_name">

And your button

<button type="submit" name="submit"></button>
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • I've updated my code to suit but it still does not work. Is there a line of code I can enter to enable error messages? – Dan Jul 28 '15 at 10:47
  • You can make var_dump before the if() statement: `var_dump(isset($_POST['submit'])); var_dump($_POST['file_name');` . What this show to you? – Marcos Pérez Gude Jul 28 '15 at 10:50
  • 'if() var_dump(isset($_POST['submit'])); var_dump($_POST['file_name'); { $file_name = $_POST['file_name']; $doc_email = $_POST['doc_email']; $doc_message = $_POST['doc_message'];' – Dan Jul 28 '15 at 11:09
  • sorry I was editing code and pasted wrong. Here is the results from $_POST["submit"] array(4) { ["file_name"]=> string(14) "_375538354.pdf" ["doc_email"]=> string(30) "daniel@example.com" ["doc_message"]=> string(0) "" ["submit"]=> string(0) "" } – Dan Jul 28 '15 at 11:14
  • So filename and submit are isset ? You need to check if the code enters in `if()` statement. If you have an empty blank page it is very sure that this if() isn't run. You can add `ini_set('display_errors', "on");` to check if there is a parse or fatal error. – Marcos Pérez Gude Jul 28 '15 at 11:20
  • i added the code but the result is still;- array(4) { ["file_name"]=> string(14) "_375538354.pdf" ["doc_email"]=> string(30) "daniel@example.com" ["doc_message"]=> string(0) "" ["submit"]=> string(0) "" } – Dan Jul 28 '15 at 12:34
  • This is correct. You receive all data. Your problem is in another code. You need to remove `return` word in your code if you need to redirect. When you make return, all next code not run – Marcos Pérez Gude Jul 28 '15 at 12:37