Ronald Ng Ronald Ng - 5 months ago 16
HTML Question

PHPMailer File Attachment

I've been working on a website with a contact form. Everything is working fine except that the attachment isn't mailed. Here are the HTML and PHP files:

(And this is the first time I ask question in this website, I apologise for this terrible formatting, thanks in advance)



<? php
require_once 'phpmailer/PHPMailerAutoload.php';

if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email_address = $_POST['email'];
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
}
if (isset($_POST['comment'])) {
$message = $_POST['comment'];
}

$body_message = '<p>You have a new message from your website contact form.</p> </br> </br> <p> Here are the deatils:</p> </br> </br> <p>Name: </p>'.$name.
'<p>Email: </p>'.$email_address.
'<p>Phone: </p> '.$phone.
'</br> <p>Message: </p> </br>'.$message;


$mail = new PHPMailer;
$mail - > IsSMTP();
$mail - > Host = 'mail.ronaldtest.com';
$mail - > Username = 'Ronald@ronaldtest.com';
$mail - > Password = '151588';
$mail - > Port = 587;
$mail - > addAddress('ronaldng1588@gmail.com');

$mail - > setFrom('ronald@ronaldtest.com', 'Brighten Management Auto Mailer');

$mail - > Subject = 'Website Contact Form: $name';
$mail - > Body = $body_message;

if (isset($_FILES['attachmentFile']) && $_FILES['attachmentFile']['error'] == UPLOAD_ERR_OK) {
$mail - > AddAttachment($_FILES['attachmentFile']['tmp_name'],
$_FILES['attachmentFile']['name']);
}


$mail - > IsHTML(true);


if ($_POST["submit"]) {
if (!$_POST['name']) {
$error = "<br />Please enter your name";
}
if (!$_POST['email']) {
$error. = "<br />Please enter your email address";
}
if (!$_POST['phone']) {
$error. = "<br />Please enter your phone number";
}
if (!$_POST['comment']) {
$error. = "<br />Please enter a comment";
}
if ($error) {
$result = '<div class="alert alert-danger" There were error(s)
in your form: '.$error.
'</div>';
} else {
if (!$mail - > Send()) {
$result = '<div class="alert alert-danger">Sorry, there was
an error sending your message. Please try again later.</div>';
} else {
$result = '<div class="alert alert-success">Thank you, <strong>'.$_POST['name'].
'</strong>! I will be in touch!</div';;
}
}
}


?>


<section id="contact">

<? php include ( 'email.php'); ?>

<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="feature_header text-center">
<h3 class="feature_title">Keep In <b>touch</b></h3>
<h4 class="feature_sub">Contact us for future improvements</h4>
<div class="divider"></div>
<?php if (isset($result)){ echo $result; } ?>
</div>
</div>

<div class="row">
<div class="contact_full">

<form method="post">
<div class="col-md-6 left">
<div class="left_contact">
<div class="control-group">
<div class="form-level controls">
<input type="text" name="name" class="input-block" placeholder="Your Name" value="<?php
if (isset($_POST[" name "])){
echo $_POST['name']; }?>"/>
<span class="form-icon fa fa-user"></span>
</div>

</div>

<div class="control-group">
<div class="form-level controls">
<input type="email" name="email" class="input-block" placeholder="Your Email" value="<?php
if (isset($_POST[" email "])){
echo $_POST['email']; }?>" />
<span class="form-icon fa fa-envelope-o"></span>
</div>
</div>

<div class="control-group">
<div class="form-level controls">
<input type="phone" name="phone" class="input-block" placeholder="Your Phone Number" value="<?php
if (isset($_POST[" phone "])){
echo $_POST['phone']; }?>" />
<span class="form-icon fa fa-phone"></span>
</div>

</div>

</div>
</div>

<div class="col-md-6 right">
<div class="control-group">
<div class="form-level controls">
<input type="file" name="attachmentFile" id="attachmentFile" value="<?php echo $_FILES['attachmentFile']['tmp_name'] ?>" />
<span class="fa fa-file"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<textarea class="textarea-block" name="comment" rows="4" value="<?php
if (isset($_POST[" comment "])){
echo $_POST['comment']; }?>"></textarea>
<span class="form-icon fa fa-pencil"></span>
</div>
</div>
</div>

<div class="col-md-12 text-center">

<input type="submit" name="submit" class="btn btn-main featured" value="Submit Now" />
</div>

</form>

</div>
</div>


</div>
</div>
</section>

Answer

for a form to upload a file it has to have enctype="multipart/form-data" in the form tag. so in your case:

 <form method="post" enctype="multipart/form-data">
Comments