0

I for the life of me cannot get this attachment to send. I have followed the tutorials on Git Hub but keeping getting "Undefined variable: mail in /Applications/XAMPP/xamppfiles/htdocs/test/PHPMailer.php on line 41" because its not hitting the second if statement at all. This is because $uploadfile is "bool(false)" (i ran a var dump). Basically my question is why isn't $uploadfile receiving anything? Any help would be appreciated.

HTML code:

<html>
<head>
<link rel ="stylesheet" type = "text/css" href = "style.css" />
</head>
 <div class="form-style-8">

<body>
<form action= "PHPMailer.php" method ="POST" id="from" enctype="multipart/form-data"> 

 <input type="text" name="fname" placeholder="Your Name"/> 
 <input type="number" name="phoneNum" placeholder="Phone Number"/>
 <input type="email" name="email" placeholder="Email Address"/>
 <input type="text" name="desc" placeholder="Leave Description"/>
 <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
 <input type="file" name="userfile" placeholder="attachment" />

<input type="submit" value="submit" />

</form>

</div>
</body>
</html>

PHP code:

<?php
require 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.phpmailer.php';


if (array_key_exists('userfile', $_FILES)) {
    // First handle the upload
    // Don't trust provided filename - same goes for MIME types
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation

    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
    var_dump($uploadfile);
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

        $mail = new PHPMailer;
        $mail->isSMTP();                                     
        $mail->Host = 'smtp.johnshopkins.edu';  
        $mail->SMTPAuth = false;                              
        $mail->Username = '';                
        $mail->Password = '';                          
        $mail->SMTPSecure = 'TLS';                           
        $mail->Port = 25;                                    
        $mail->setFrom('abrown5991@gmail.com', 'Mailer');
        $mail->addAddress('andrewb7@vt.edu', 'Joe User');   


        $mail->isHTML(true);                             
        $fnew =$_POST["fname"];
        $phone =$_POST["phoneNum"];
        $newEmail =$_POST["email"];
        $newDesc =$_POST["desc"];

        $mail->AddAttachment( $uploadfile, 'my file');

        $message = $str = implode(' ', array($fnew,$phone,$newEmail,$newDesc)); 
        $mail->Subject = 'Auto Email';
        $mail->Body    = $message;



if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
}
} 
Ben
  • 11
  • 1
  • Is tempnam() returning anything? – Jason K Jun 23 '17 at 14:26
  • no its also returning "bool(false)" – Ben Jun 23 '17 at 14:30
  • Considering tempnam() fills in $uploadfile. Thats the code that needs to be looked at. The next if statement is test if a move worked. if $_FILES['userfile']['tmp_name'] has a valid file name it can move it to a file. You should also be testing for $uploadfile !== false. – Jason K Jun 23 '17 at 14:40
  • Thanks. Just checked and both "sha1($_FILES['userfile']['name'])" and "sys_get_temp_dir()" return the proper strings but when they're put into tempnam() its somehow false. Any idea? – Ben Jun 23 '17 at 14:44
  • Check you web servers error log or turn on php error reporting https://stackoverflow.com/a/16933753/4178487 – Jason K Jun 23 '17 at 14:54

0 Answers0