I know there are many questions/answers to this on here as well as all over Google, and I've looked through many but can't seem to find a solution that works for me, tried all but failed. Feel free to rate negative if you think I haven't searched hard enough.
I'm new to PHP, and this is the first time I've needed to use file attachments so it's foreign to me.
Can someone help?
Here is the PHP:
<?php
if ($_POST["application-submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$picture = $_POST['picture'];
$to = 'models@swoleinc.com';
$subject = 'Swole Inc Application';
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
$body ="Name: $name
E-Mail: $email
Age: $age
Height: $height
Weight: $weight
Picture: $picture
IP Address: $ip";
if (!$_POST['name']) {
$errName = '^ You forgot your name!';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = '^ We need a valid email.';
}
if (!$_POST['age']) {
$errAge = '^ How old are you?';
}
if (!$_POST['height']) {
$errHeight = '^ Tell us how tall you are.';
}
if (!$_POST['weight']) {
$errWeight = '^ We promise we won\'t laugh!';
}
if (!$_POST['picture']) {
$errPicture = '^ Show us your physique!';
}
if (!$errName && !$errAge && !$errHeight && !$errWeight && !$errEmail && !$errPicture) {
if (mail ($to, $subject, $body)) {
$result='<div class="alert alert-success">Thanks for applying! If you fit our current needs, we\'ll be in touch soon!</div>';
} else {
$result='<div class="alert alert-danger">Sorry, something\'s broken. If the problem persists, email models@swoleinc.com instead.</div>';
}
}
}
?>
Here is the HTML:
<?php include('apply.php'); ?>
<form action="#apply" method="post" id="apply" enctype="multipart/form-data">
<div class="text-center">
<i class="text-muted">All fields are required.</i><p style="margin-bottom: 15px"></p>
</div>
<div class="group">
<input type="text" name="name" id="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<span class="highlight"></span>
<span class="bar"></span>
<?php echo "<p class='alert-danger'>$errName</p>";?>
<label>Name</label>
</div>
<div class="group">
<input type="text" name="email" id="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<span class="highlight"></span>
<span class="bar"></span>
<?php echo "<p class='alert-danger'>$errEmail</p>";?>
<label>Email</label>
</div>
<div class="group">
<input type="text" class="extra" name="age" id="age" value="<?php echo htmlspecialchars($_POST['age']); ?>">
<span class="highlight"></span>
<span class="bar"></span>
<?php echo "<p class='alert-danger'>$errAge</p>";?>
<label>Age</label>
</div>
<div class="group">
<input type="text" class="extra" name="height" id="height" value="<?php echo htmlspecialchars($_POST['height']); ?>">
<span class="highlight"></span>
<span class="bar"></span>
<?php echo "<p class='alert-danger'>$errHeight</p>";?>
<label>Height</label>
</div>
<div class="group">
<input type="text" class="extra" name="weight" id="weight" value="<?php echo htmlspecialchars($_POST['weight']); ?>">
<span class="highlight"></span>
<span class="bar"></span>
<?php echo "<p class='alert-danger'>$errWeight</p>";?>
<label>Weight</label>
</div>
<div class="group">
<input type="file" name="picture" allow="images/*" style="border-bottom: 0; padding: 0" value="<?php echo htmlspecialchars($_POST['picture']); ?>">
<?php echo "<p class='alert-danger'>$errPicture</p>";?>
</div>
<div class="group">
<?php echo $result; ?>
</div>
<div class="group">
<input id="application-submit" type="submit" name="application-submit" class="btn-apply text-center" value="I have what it takes!" style="display: block; padding: 20px 30px">
</div>
</form>
Feel free to view the live version: Swole Inc
As you will see, the image won't send right now which obviously won't let the form submit because it'll give the error message.
EDIT:
This was one of the variations I tried:
<?php
if ($_POST["application-submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$height = $_POST['height'];
$weight = $_POST['weight'];
$picture = $_POST['picture'];
$to = 'models@swoleinc.com';
$subject = 'Swole Inc Application';
$file = $path . "/" . $filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = PHP_EOL;
$headers = "From: name <models@swoleinc.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$headers .= $message . $eol . $eol;
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol . $eol;
$headers .= $content . $eol . $eol;
$headers .= "--" . $separator . "--";
$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
$body ="Name: $name
E-Mail: $email
Age: $age
Height: $height
Weight: $weight
Picture: $picture
IP Address: $ip";
if (!$_POST['name']) {
$errName = '^ You forgot your name!';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = '^ We need a valid email.';
}
if (!$_POST['age']) {
$errAge = '^ How old are you?';
}
if (!$_POST['height']) {
$errHeight = '^ Tell us how tall you are.';
}
if (!$_POST['weight']) {
$errWeight = '^ We promise we won\'t laugh!';
}
if (!$_POST['picture']) {
$errPicture = '^ Show us your physique!';
}
if (!$errName && !$errAge && !$errHeight && !$errWeight && !$errEmail && !$errPicture) {
if (mail ($to, $subject, $body, $headers)) {
$result='<div class="alert alert-success">Thanks for applying! If you fit our current needs, we\'ll be in touch soon!</div>';
} else {
$result='<div class="alert alert-danger">Sorry, something\'s broken. If the problem persists, email models@swoleinc.com instead.</div>';
}
}
}
?>