1

Hello I have a problem with my email form I just want to attach the possibility to send an attachment with this form in the HTML I add already the possibility with the button but how to proceed it in the PHP file? Can somebody help please?

the Email Form:

<form id="booking-form" class="booking-form" name="form1" method="post" action="">
            <div align="center"><img class="logo" src="img/example_logo.png" title="Example Logo" alt="Example Logo"></div>
            <div class="h1"><strong>Kontaktieren Sie uns</strong></div>
            <div id="form-message" class="message hide">
                Vielen Dank für Ihre Anfrage, wir werden uns so schnell wie möglich bei Ihnen melden, um Ihre Anfrage zu bearbeiten<br><a class="" href="../index.html">zurück zur Homepage</a>

            </div></div>
            <div id="form-content">
                <div class="group">
                    <label for="name">Name:</label>
                    <div><input id="name" name="name" class="form-control" type="text" placeholder=""></div>
                </div>
                <div class="group">
                    <label for="email">Email:</label>
                    <div><input id="email" name="email" class="form-control" type="email" placeholder=""></div>
                </div>
                <div class="group">
                    <label for="phone">Telefon:</label>
                    <div><input id="phone" name="phone" class="form-control" type="number" placeholder=""></div>
                </div>
                <div class="group">
                    <label for="special-requirements">Nachricht:</label>
                    <div><textarea id="special-requirements" name="special-requirements" class="form-control" cols="" rows="5" placeholder=""></textarea></div>
                </div>
                <form action="/action_page.php">
                <input type="file" id="myFile" name="filename">
                
                <div class="group">
                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="AGB" value="OK" checked>Hiermit akzeptiere ich die <a href="../DSGVO.html">DSGVO</a>
                
                <div class="group submit">
                    <label class="empty"></label>
                    <div><input name="submit" type="submit" value="Absenden"/></div>
                </div>
            </div>
            <div id="form-loading" class="hide"><i class="fa fa-circle-o-notch fa-spin"></i></div></div>
        </form>

And the PHP script is this:

<?php
/**
 * 
 */

define('_EMAIL_TO', 'xxx@xxx.xx'); // your email address where reservation details will be received
define('_EMAIL_SUBJECT', 'XXX'); // email message subject
define('_EMAIL_FROM', $_POST["email"]);

$fields = array(
    array('name' => 'AGB', 'title' => 'AGB '),
    array('name' => 'name', 'title' => 'Name', 'valid' => array('require')),
    array('name' => 'phone', 'title' => 'Telefon', 'valid' => array('require')),
    array('name' => 'email', 'title' => 'Email', 'valid' => array('require')),
    array('name' => 'special-requirements', 'title' => 'Special requirements')
);

$error_fields = array();
$email_content = array();
foreach ($fields AS $field){
    $value = isset($_POST[$field['name']])?$_POST[$field['name']]:'';
    $title = empty($field['title'])?$field['name']:$field['title'];
    $email_content[] = $title.': '.nl2br(stripslashes($value));
    $is_valid = true;
    $err_message = '';
    if (!empty($field['valid'])){
        foreach ($field['valid'] AS $valid) {
            switch ($valid) {
                case 'require':
                    $is_valid = $is_valid && strlen($value) > 0;
                    $err_message = 'Field required';
                    break;
                case 'email':
                    $is_valid = $is_valid && preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $value);
                    $err_message = 'Email required';
                    break;
                default:                
                    break;
            }
        }
    }
    if (!$is_valid){
        if (!empty($field['err_message'])){
            $err_message = $field['err_message'];
        }
        $error_fields[] = array('name' => $field['name'], 'message' => $err_message);
    }
}

if (empty($error_fields)){
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers = "From: "._EMAIL_FROM."\r\n"; 
    $headers .= "Reply-To: "._EMAIL_FROM."\r\n";    
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    // Send email
    mail (_EMAIL_TO, _EMAIL_SUBJECT, implode('<hr>', $email_content), $headers);
    echo (json_encode(array('code' => 'success')));
}else{
    echo json_encode(array('code' => 'failed', 'fields' => $error_fields));
}

Is it possible to just add some code to the php script that would send also the attachemend to the defined mail? And how can i limit the max file size to e.g. 5MB?

Thank you very much for your help.

  • You would need to assemble the multipart mail body manually, if you want to do it using `mail`. Complex, and error prone. Use a library like PHPMailer instead. – CBroe Aug 23 '22 at 07:17
  • Does this answer your question? [Send attachments with PHP Mail()?](https://stackoverflow.com/questions/12301358/send-attachments-with-php-mail) – Rohit Gupta Aug 29 '22 at 00:02
  • Hi, thanks for the answer so i will take a look at the PHP Mailer... So there is no other way maybe to adapt o just change a little bit my code and use it instead of use the PHPMailer? – Johannes Pohle Aug 30 '22 at 10:25

0 Answers0