在电子邮件

时间:2015-07-12 22:43:18

标签: php

我知道这里有很多问题/答案以及谷歌的所有问题,而且我已经看了很多,但似乎无法找到适合我的解决方案失败。如果您认为我没有足够的搜索,请随意评价为负数。

我是PHP的新手,这是我第一次需要使用文件附件,因此对我来说很陌生。

有人可以帮忙吗?

这是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\n\n
    E-Mail: $email\n\n 
    Age: $age\n\n 
    Height: $height\n\n
    Weight: $weight\n\n
    Picture: $picture\n\n
    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>';
        }
    }
}
?>

这是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>

随意查看实时版本:Swole Inc

正如您将看到的,图片现在无法发送,显然不会让表单提交,因为它会给出错误消息。

编辑:

这是我尝试过的变种之一:

<?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\n\n
    E-Mail: $email\n\n 
    Age: $age\n\n 
    Height: $height\n\n
    Weight: $weight\n\n
    Picture: $picture\n\n
    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>';
        }
    }
}
?>

0 个答案:

没有答案