0

I am working on a contact form that works perfectly, but I would like to have an image upload feature so that the image is sent with the message as an attachment to the e-mail I specify. Here is my code:

<form method="post" action="contact.php">

            <label>Name</label>
            <input name="name" type="text" placeholder="">

            <label>Email</label>
            <input name="email" type="email" placeholder="">

            <label>message</label>
            <textarea name="message" placeholder=""></textarea>

            <label>How much is 2+2? (Anti-spam)</label>
            <input name="human" type="number" placeholder=""><br>

            <input id="submit" name="submit" type="submit" value="Send message!" class="button_verde"><br><br>


            <?php
                $name = $_POST['name'];
                $email = $_POST['email'];
                $message = $_POST['message'];
                $from = 'Website'; 
                $to = 'mail@mail.com'; 
                $subject = 'New message';
                $human = $_POST['human'];


                $headers = "MIME-Version: 1.0" . PHP_EOL;
                $headers .= "From: $from <$email> ". PHP_EOL;
                $headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;

                $name = str_replace( '[at]','@', $name);
                $message = str_replace( '[at]','@', $message);

                $body = "<strong>Från:</strong> $name\n <br><strong>E-post:</strong> $email\n <br><strong>Meddelandet:</strong>\n <br>$message";


                if ($_POST['submit']) {
                if ($name != '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    if ($human == '4') {                 
                        if (mail ($to, $subject, $body, $headers)) { 
                        echo '<div data-alert class="alert-box success radius">
                                Message sent!<a href="#" class="close">&times;</a>
                              </div>';
                    } else { 
                        echo '<div data-alert class="alert-box warning round">
                                Try again!<a href="#" class="close">&times;</a>
                              </div>'; 
                    } 
                } else if ($_POST['submit'] && $human != '4') {
                    echo '<div data-alert class="alert-box warning round">
                            Wrong answer!<a href="#" class="close">&times;</a>
                          </div>';
                }
                } else {
                    echo '<div data-alert class="alert-box warning round">
                            Missing obligatory fields!<a href="#" class="close">&times;</a>
                          </div>';
                }
            }

            ?>
        </form>

My question is, how can I add an image upload to this form?

halfer
  • 19,824
  • 17
  • 99
  • 186
viriato
  • 859
  • 2
  • 13
  • 26
  • 2
    `enctype:'multipart/form-data'` is missing in your form – Funk Forty Niner Feb 24 '14 at 15:24
  • Rather use some library, attaching files with ``mail()`` is no easy task. http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – arma Feb 24 '14 at 15:25
  • Actually, there was a bit of a typo (`:`) in my first comment. `
    ` (`enctype="multipart/form-data"`) which is essential for image uploading/attachment. There are many scripts available online to do just what you're looking to do. However, using PHPmailer or Swiftmailer will facilitate the task.
    – Funk Forty Niner Feb 24 '14 at 15:33
  • Don't build your own mime emails. Use PHPmailer or Swiftmailer. Both make it utterly trivial to generate mime emails with attachments. – Marc B Feb 24 '14 at 15:40

0 Answers0