2017-04-02 1 views
0

J'ai créé un contact pour avec html et en utilisant le code PHP pour envoyer mon email et son fonctionnement bien mais j'ai un problème que je veux joindre le fichier et à envoyer aussi mais je ne ' sais pas comment tout le monde peut me aider .... et le code php est ci-dessous veux juste ajouter la pièce jointe des fichiers grâce à l'avancejoindre le fichier au formulaire de contact PHP

<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Untitled Document</title> 
 
</head> 
 

 
<body> 
 

 
<!-- Start PHP CODE --> 
 
<?php 
 
error_reporting(E_ALL); 
 
ini_set('display_errors', 1); 
 
// define Errors variables 
 
$fnameErr = $lnameErr = $emailErr = $humanErr = $result = "" ; 
 
if(isset($_POST['submit'])) 
 
{ 
 
// define contact form variables 
 
$fname = $_POST['fname']; 
 
$lname = $_POST['lname']; 
 
$email = $_POST['email']; 
 
$tel = $_POST['tel']; 
 
$design = $_POST['design']; 
 
$country = $_POST['country']; 
 
$comment = $_POST['comment']; 
 
$human = $_POST['human']; 
 
// define Errors variables 
 
$fnameErr = $lnameErr = $emailErr = $humanErr = "" ; 
 
// define Checks variables 
 
$check1 = $check2 = $check3 = $check4 = ""; 
 
// define email variables 
 
$to = '[email protected]'; 
 
$subject = 'Contact Form'; 
 
$message = 'From: '.$fname .$lname."\r\n". 
 
      'E-mail: '.$email."\r\n". 
 
      'Telephone: '.$tel."\r\n". 
 
      'Designation: '.$design."\r\n". 
 
      'Country Appled From: '.$country."\r\n". 
 
      'Message: '.$comment."\r\n"; 
 
$headers = "MIME-Version: 1.0" . "\r\n"; 
 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
 
$headers = "From:" .$email. "\n" ; \t 
 
\t 
 
// Let's do some checks \t 
 
// Checking the First Name 
 
if(empty($_POST["fname"])){ 
 
\t $fnameErr = "Name is Required"; 
 
}else{ 
 
\t $fname = test_input($_POST["fname"]); 
 
\t // check if name only contains letters and whitespace 
 
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) { 
 
     $fnameErr = "Only letters and white space allowed"; 
 
    }else{ 
 
\t \t $check1 = 1; 
 
\t } 
 
} 
 
// Checking the Last Name \t 
 
if(empty($_POST["lname"])){ 
 
\t $lnameErr = "Name is Required"; 
 
}else{ 
 
\t $lname = test_input($_POST["lname"]); 
 
\t // check if name only contains letters and whitespace 
 
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) { 
 
     $lnameErr = "Only letters and white space allowed"; 
 
    }else{ 
 
\t \t $check2 = 1; 
 
\t } 
 
} 
 
//Checking the Email Adress 
 
if(empty($_POST["email"])){ 
 
\t $emailErr = "Email is Required"; 
 
}else{ 
 
\t $email = test_input($_POST["email"]); 
 
    // check if e-mail address is well-formed 
 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
 
     $emailErr = "Invalid email format"; 
 
    }else{ 
 
\t \t $check3 = 1; 
 
\t } 
 
} 
 
//Checking the Anti-Spam Question 
 
if(empty($_POST["human"])){ 
 
\t $humanErr = "Please Enter the Answer"; 
 
}else{ 
 
\t if ($human != 4){ 
 
\t \t $humanErr = "Please check your answer"; 
 
\t }else{ 
 
\t \t $check4 = 1; 
 
\t } 
 
} 
 
\t 
 
// Emailing the Contents if all Checks are correct \t 
 
if($check1 && $check2 && $check3 && $check4 == 1){ 
 
\t mail($to, $subject, $message, $headers); 
 
\t $result = "Message Sent Sucessfully"; 
 
}else{ 
 
\t $result = "Message Can't be sent"; 
 
} 
 
} 
 
function test_input($data) { 
 
    $data = trim($data); 
 
    $data = stripslashes($data); 
 
    $data = htmlspecialchars($data); 
 
    return $data; 
 
} 
 
?> 
 
<!-- END OF PHP CODE --> 
 

 

 
<h2>Contact Form</h2> 
 
<p><span style="color: red" >*Required field</span></p> 
 
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> 
 
\t First Name:<input type="text" name="fname"><span style="color: red" >*</span> <?php echo $fnameErr ?> <br><br> 
 
\t Last Name:<input type="text" name="lname"><span style="color: red" >*</span> <?php echo $lnameErr ?> <br><br> 
 
\t E-mail:<input type="text" name="email"><span style="color: red" >*</span> <?php echo $emailErr ?> <br><br> 
 
\t Telephone:<input type="text" name="tel"><br><br> 
 
\t Designation:<select name="design"> 
 
    \t \t <option value="Architectural Engineer">Architectural Engineer</option> 
 
    \t \t <option value="Structural Engineer">Structural Engineer</option> 
 
    \t \t <option value="Draughts-man">Draughts-man</option> 
 
    \t \t <option value="Receptionist">Receptionist</option> 
 
    \t \t <option value="Secertary">Secertary</option> 
 
\t </select><br><br> 
 
\t Country Applied From:<select name="country"> 
 
\t \t <option value="">Country...</option> 
 
\t \t <option value="Afganistan">Afghanistan</option> 
 
\t \t <option value="Albania">Albania</option> 
 
</select><br><br> 
 
\t Message:<textarea name="comment"></textarea> <br><br> 
 
\t Upload Your Resume:<span style="color: red" >*</span><input type="file" name="uploaded_file"><br><br> \t 
 
\t <label>*What is 2+2? (Anti-spam)</label> 
 
\t <input name="human" placeholder="Type Here"><span style="color: red" >*</span><?php echo $humanErr ?><br><br> 
 
\t <input type="submit" name="submit" value="Submit"> 
 
\t <input type="reset" value="Clear"> 
 
<?php echo $result ?> 
 
</form><br> 
 

 
</body> 
 
</html>

+0

Vous pouvez envoyer une photo ci-jointe en utilisant «pièce jointe» lors de l'envoi –

Répondre

3

Vous pouvez utiliser le PHPMailer bibliothèque pour envoyer facilement des e-mails avec des pièces jointes

$email = new PHPMailer(); 
$email->From  = '[email protected]'; 
$email->FromName = 'From Name'; 
$email->Subject = 'Your Email Subject'; 
$email->Body  = $bodytext; 
$email->AddAddress('[email protected]'); 

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE'; 

$email->AddAttachment($file_to_attach , 'NameOfFile.pdf'); 

return $email->Send(); 

Assurez-vous d'inclure la bibliothèque PHPMailer. Vous pouvez obtenir cette bibliothèque d'ici (https://github.com/PHPMailer/PHPMailer)

selon la demande de commentaires, l'ajout de code de téléchargement et le code email avec exemple complet ci-dessous:

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 
// define Errors variables 
$fnameErr = $lnameErr = $emailErr = $humanErr = $result = "" ; 
if(isset($_POST['submit'])) 
{ 
//checking attached file and uploading it to folder 
$upload_file_path = ''; 
if (isset($_FILES['uploaded_file']) && !empty($_FILES['uploaded_file']))  
{ 
    $dir_path = getcwd();//getting current working directory path 
    $upload_dir = $dir_path . 'uploads/'; 
    if (!file_exists($upload_dir)) 
    { 
     mkdir($upload_dir, 0777, true); 
    } 
    $file_name = $_FILES['uploaded_file']['name']; 
    $ext = pathinfo($file_name, PATHINFO_EXTENSION); 

    //validating uploaded file extension 
    $valid_extensions = array('pdf', 'doc');//You can customize extension's here 

    if (!in_array($ext, $valid_extensions)) 
    { 
     exit('Please use .pdf or .doc file to upload CV.'); 
    } 
    $upload_file_path = $upload_dir . $file_name; 

    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $upload_file_path);  

} 


// Let's do some checks 
// Checking the First Name 
if(empty($_POST["fname"])){ 
    $fnameErr = "Name is Required"; 
}else{ 
    $fname = test_input($_POST["fname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) { 
     $fnameErr = "Only letters and white space allowed"; 
    }else{ 
     $check1 = 1; 
    } 
} 
// Checking the Last Name 
if(empty($_POST["lname"])){ 
    $lnameErr = "Name is Required"; 
}else{ 
    $lname = test_input($_POST["lname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) { 
     $lnameErr = "Only letters and white space allowed"; 
    }else{ 
     $check2 = 1; 
    } 
} 
//Checking the Email Adress 
if(empty($_POST["email"])){ 
    $emailErr = "Email is Required"; 
}else{ 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $emailErr = "Invalid email format"; 
    }else{ 
     $check3 = 1; 
    } 
} 
//Checking the Anti-Spam Question 
if(empty($_POST["human"])){ 
    $humanErr = "Please Enter the Answer"; 
}else{ 
    if ($human != 4){ 
     $humanErr = "Please check your answer"; 
    }else{ 
     $check4 = 1; 
    } 
} 

// Emailing the Contents if all Checks are correct 
if($check1 && $check2 && $check3 && $check4 == 1){ 
    mail($to, $subject, $message, $headers); 
    $result = "Message Sent Sucessfully"; 
}else{ 
    $result = "Message Can't be sent"; 
} 
} 
function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

// define contact form variables 
$fname = $_POST['fname']; 
$lname = $_POST['lname']; 
$email = $_POST['email']; 
$tel = $_POST['tel']; 
$design = $_POST['design']; 
$country = $_POST['country']; 
$comment = $_POST['comment']; 
$human = $_POST['human']; 
// define Errors variables 
$fnameErr = $lnameErr = $emailErr = $humanErr = "" ; 
// define Checks variables 
$check1 = $check2 = $check3 = $check4 = ""; 
// define email variables 
$to = '[email protected]'; 
$subject = 'Contact Form'; 
$message = 'From: '.$fname .$lname."\r\n". 
      'E-mail: '.$email."\r\n". 
      'Telephone: '.$tel."\r\n". 
      'Designation: '.$design."\r\n". 
      'Country Appled From: '.$country."\r\n". 
      'Message: '.$comment."\r\n"; 

$email = new PHPMailer(); 
$email->From  = '[email protected]';//Your From Email 
$email->FromName = $fname; 
$email->Subject = $subject; 
$email->Body  = $message; 
$email->AddAddress($email); 

if (!empty($upload_file_path)) 
{ 
    $email->AddAttachment($upload_file_path , 'CV_'.time().'.pdf'); 
} 

return $email->Send(); 



?> 

Essayez et laissez-moi savoir si vous avez toute question. Assurez-vous également d'inclure la bibliothèque phpmailer au-dessus de votre fichier php.

+0

@ Mahesh Singh Chouhan actaully je veux joindre pdf et doc pour cv aussi je sais à propos de php mailer mais vraiment je ne sais pas comment l'utiliser c'est pourquoi J'ai demandé le code php ou sinon si vous avez une référence à php mailer s'il vous plaît envoyez-moi .... merci pour votre aide vraiment – paulrf

+0

Voulez-vous que je ajoute le code de fichier «upload» trop avec le code email? Comme je peux le voir, vous n'avez pas ajouté le code de téléchargement de fichier php dans votre code php. –

+0

ce que j'ai besoin est de joindre un fichier avec ce formulaire de contact afin qu'il sera envoyé à mon courrier lorsque l'utilisateur presse soumettre .... donc je ne sais pas si son nécessaire pour le télécharger et téléchargé où ... j'ai regardé cent de vidéos mais rien n'a aidé tout ce dont j'ai besoin est d'attacher un fichier pdf ou doc ​​ou docx quand l'utilisateur presse soumettre je peux recevoir le fichier sur mon pc .... et vraiment merci pour votre patience je l'apprécie – paulrf