article

Thursday, December 1, 2016

PHPMailer Upload mail Attachment

PHPMailer Upload mail Attachment

PHPMailer-master https://github.com/PHPMailer/PHPMailer

<?php
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
 if (preg_match('/\.(doc|docx|xls|xlsx|pdf|jpeg|png)$/i', $_FILES["userfile"]["name"])) //upload
 {
  $uploadfile = $_FILES['userfile']['name'];
  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        // This should be somewhere in your include_path
        require 'PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('from@example.com', 'First Last');
        $mail->addAddress('tomail@example.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->msgHTML("My message body");
        $mail->addAttachment($uploadfile);
        if (!$mail->send()) {
            $msg = "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg = "Message sent!";
        } 
  }else {
   $msg = '<font color=red> ERROR_FAILED_TO_UPLOAD_FILE </font>';
  }
 }else{
  $msg = '<font color=red> ERROR_FAILED_TO_UPLOAD_FILE </font>';
 } 
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>PHPMailer Upload mail Attachment</title>
</head>
<body>
<?php if (empty($msg)) { ?>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file">
        <input type="submit" value="Send File">
    </form>
<?php } else {
    echo $msg;
} ?>
</body>
</html>

Related Post