0

I have a code in PHP sending the message with an attachment. Code works fine instead of attachment. When I attach the file and click "Wyślij wiadomość" (Send) I have a message in my mailbox but there`s nothing attached. I think that I did something wrong with the place I put $formFile

 $mailText = "Treść wiadomości:\n$formText\nOd: $formName, $formEmail, $formFile, ($ip, $host)";

Here`s the full code:

<?php
//--- początek formularza ---
if(empty($_POST['submit'])) {
?>
            <form action="" method="post" enctype="multipart/form-data">
                        <table class="col-md-10 col-sm-12 col-xs-12">
                            <tr>
                                <td class="col-md-2">NAZWISKO:<br /><br/></td>
                                <td><input type="text" name="formName" /></td>
                            </tr>
            <tr>
                            <td class="col-md-2">E-MAIL:<br /><br/></td>
                            <td><input type="text" name="formEmail"/></td>
                        </tr>
                        <tr>
                            <td class="col-md-2">ZAŁĄCZ KOSZTORYS:<br /><br/></td>
                            <td><input type="file" name="formFile" /></td>
                        </tr>
                        <tr>
                            <td class="formularzTresc col-md-2">TREŚĆ:<br /><br/></td>
                            <td><textarea name="formText"></textarea></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><p align="right"><input type="submit" name="submit" value="Wyślij wiadomość" /><p></td>
                        </tr>
                        </table>
                    </form>
<?php
} else {

//twoje dane
$email = 'e-mail';

//dane z formularza
$formName = $_POST['formName'];
$formEmail = $_POST['formEmail'];
$formText = $_POST['formText'];
$formFile = $_FILES['formFile'];

if(!empty($formName) && !empty($formEmail) && !empty($formText)) {

//--- początek funkcji weryfikującej adres e-mail ---
function checkMail($checkmail) {
  if(filter_var($checkmail, FILTER_VALIDATE_EMAIL)) {
    if(checkdnsrr(array_pop(explode("@",$checkmail)),"MX")){
        return true;
      }else{
        return false;
      }
  } else {
    return false;
  }
}
//--- koniec funkcji ---
if(checkMail($formEmail)) {
  //dodatkowe informacje: ip i host użytkownika
  $ip = $_SERVER['REMOTE_ADDR'];
  $host = gethostbyaddr($_SERVER['REMOTE_ADDR']);

  //tworzymy szkielet wiadomości
  //treść wiadomości
  $mailText = "Treść wiadomości:\n$formText\nOd: $formName, $formEmail, $formFile, ($ip, $host)";

  //adres zwrotny
  $mailHeader = "From: $formName <$formEmail>\r\n";
    $mailHeader .= "Content-type: text/plain; charset=utf-8\r\n";
        $target_path = "../przeslanePliki";
        $target_path = $target_path . basename( $_FILES['formFile']['name']);
if(move_uploaded_file($_FILES['formFile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['formFile']['name']). 
    " has been sent. ";

} else{
    echo "Problem with sending the file.";
}

  //funkcja odpowiedzialna za wysłanie e-maila
  @mail($email, 'Pytanie do eksperta', $mailText, $mailHeader) or die('Błąd: wiadomość nie została wysłana');

  //komunikat o poprawnym wysłaniu wiadomości
  echo 'Wiadomość została wysłana';
} else {
  echo 'Adres e-mail jest niepoprawny';
}

} else {
  //komunikat w przypadku nie powodzenia
  echo 'Wypełnij wszystkie pola formularza';
}

//--- koniec formularza ---
}
?>
Aviene
  • 1
  • 2
  • people recommand using PHPMailer, have you considered this option? http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – maalls Apr 26 '15 at 11:08
  • I didn`t know about it, but there`s have to be a really small bug in my code and everything will be working fine. – Aviene Apr 26 '15 at 12:46
  • How do you know its a small bug ? In the link I shared if you look at the seconde answer by Pragnesh Chauhan, it shows how you can send an email using mail() function like want to do. – maalls Apr 26 '15 at 16:00

0 Answers0