Copy link to clipboard
Copied
Hello Experts,
Please forgive me, but I'm not well-versed in php. What I am trying to do (unsucessfully so far) is add a simple "upload attachment" feature (for images, or other documents) to my existing form so when the user clicks the submit button I get the email with attachments.
It's a very simple form that looks like this:
<!--begin HTML Form-->
<form role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!--when submit button is clicked, show results here-->
<div class="form-group"> <?php echo $result;?> </div>
<div class="form-group">
<textarea class="form-control" rows="3" name="message" requiredplaceholder="What's new?"><?php echo $message;?></textarea>
<span class="required small"><?php echo $errMessage;?></span> </div>
<div class="form-group">
<h6 class="human">A number between 1 and 1,806,010</h6>
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<?php echo $human;?>">
<span class="required small"><?php echo $errHuman;?></span><br>
<button type="submit" id="submit" name="submit" class="btn btn-success">SUBMIT</button>
<button type="reset" id="reset" name="reset" class="btn btn-success">RESET</button>
</div>
</div>
And in the head I have the following:
<?php
// NOTE: this page must be saved as a .php file.
// And your server must support PHP 5.3+ PHP Mail().
// Define variables and set to empty values
$result = $message = $human = "";
$errName = $errMessage = $errHuman = "";
if ( isset( $_POST[ "submit" ] ) ) {
$message = $_POST[ 'message' ];
$human = intval( $_POST[ 'human' ] );
//valid address on your web server
$from = 'webmaster@mywebsite.info';
//your email address where you wish to receive mail
$to = 'me@mywebsite.com';
$subject = 'MESSAGE FROM YOUR WEB SITE';
$headers = "From:$from\r\nReply-to:$email";
$body = "Message: $message";
//Check if message is entered
if ( empty( $_POST[ "message" ] ) ) {
$errMessage = "Please enter your message.";
} else {
$message = test_input( $_POST[ "message" ] );
}
//Check if simple anti-bot test is entered
if ( empty( $_POST[ "human" ] ) ) {
$errHuman = "Please enter the sum.";
} else {
if ( $human !== 13 ) {
$errHuman = 'Wrong answer. Please try again.';
}
}
// If there are no errors, send the email & output results to the form
if ( !$errMessage && !$errHuman ) {
if ( mail( $to, $subject, $body, $from ) ) {
$result = '<div class="alert alert-success"><h2><span class="glyphicon glyphicon-ok"></span> Message sent!</h2><h3>Thank you for contacting us. Someone will be in touch with you soon.</h3></div>';
} else {
$result = '<div class="alert alert-danger"><h2><span class="glyphicon glyphicon-warning-sign"></span> Sorry there was a form processing error.</h2> <h3>Please try again later.</h3></div>';
}
}
}
//sanitize data inputs
function test_input( $data ) {
$data = trim( $data );
$data = stripslashes( $data );
$data = htmlspecialchars( $data );
$data = ( filter_var( $data, FILTER_SANITIZE_STRING ) );
return $data;
}
//end form processing script
?>
Any assistance is very much appreciated.
Thanks!
Dave
Copy link to clipboard
Copied
PHPMailer can handle attachments.
https://github.com/PHPMailer/PHPMailer