2014-06-05 2 views
-1

나는 데이터베이스에 데이터를 저장하고 이력서와 같은 파일을 첨부하는 사용자로부터 클라이언트에게 전자 메일을 보내는 PHP 코드를 작성했습니다. 아래 코드는 데이터를 데이터베이스에 저장합니다. 또한 양식 필드를 클라이언트에게 전자 메일로 보내면 누구든지 메일을 보내는 동안 파일 첨부를 도와 줄 수 있습니다.PHP를 통해 메일에 파일을 첨부하는 방법

<?php 
include_once "dbconnection.php"; 
if(isset($_FILES['file']['name'])){ 
$ext = end(explode('.', $_FILES['file']['name'])); 
$ext; 
$target = "careers"; 
$il = $_FILES['file']['name']; 
$target = $target . $_FILES['file']['name']; 

if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
{ 
} 
else 
{ 
} 
} 
if(isset($_POST["role"])){ 
    $role=$_POST["role"];} else {$role="";} 

if(isset($_POST["fname"])){ 
$fname=$_POST["fname"];} else {$fname="";} 

if(isset($_POST["lname"])){ 
$lname=$_POST["lname"];} else {$lname="";} 

if(isset($_POST["city"])){ 
    $city=$_POST["city"];} else {$city="";} 

if(isset($_POST["email"])){ 
    $email=$_POST["email"];} else {$email="";} 

if(isset($_POST["cntctno"])){ 
    $cntctno=$_POST["cntctno"];} else {$cntctno="";} 

if(isset($_POST["basicqualific"])){ 
    $basicqualific=$_POST["basicqualific"];} else {$basicqualific="";} 


if(isset($_POST["postqualific"])){ 
    $postqualific=$_POST["postqualific"];} else {$postqualific="";} 

if(isset($_POST["resumeheadline"])){ 
    $resumeheadline=$_POST["resumeheadline"];} else {$resumeheadline="";} 

if(isset($_POST["expyears"])){ 
    $expyears=$_POST["expyears"];} else {$expyears="";} 

if(isset($_POST["expmonths"])){ 
    $expmonths=$_POST["expmonths"];} else {$expmonths="";} 

if(isset($_POST["currsalary"])){ 
    $currsalary=$_POST["currsalary"];} else {$currsalary="";} 

if(isset($_POST["expsalary"])){ 
    $expsalary=$_POST["expsalary"];} else {$expsalary="";} 

if(isset($_POST["curremploy"])){ 
    $curremploy=$_POST["curremploy"];} else {$curremploy="";} 

if(isset($_POST["jobtitle"])){ 
    $jobtitle=$_POST["jobtitle"];} else {$jobtitle="";} 

if(isset($_POST["preflocation"])){ 
    $preflocation=$_POST["preflocation"];} else {$preflocation="";} 

if(isset($_FILES["file"])){ 
    $file=$target;} else {echo "not set";} 
$sql="INSERT INTO careers (role, fname, lname, city, email, cntctno, basicqualific, postqualific, resumeheadline, expyears, expmonths, currsalary, expsalary, curremploy, jobtitle, preflocation, image) 
    VALUES ('$role', '$fname', '$lname', '$city', '$email', '$cntctno', '$basicqualific', '$postqualific', '$resumeheadline', '$expyears', '$expmonths', '$currsalary', '$expsalary', '$curremploy', '$jobtitle', '$preflocation', '$il')"; 


$to = "[email protected]"; 

$subject = "Contact mail through website from ".$fname." ".$lname; 

$from = "[email protected]"; 

$message = 
" 
Role: ".$role. 
" 
Name: ".$fname." ".$lname. 
" 
Email: ". $email. 
" 
Phone: ".$cntctno. 
" 
City: ".$city. 
" 
Service: ".$service. 
" 
Basic Qualification: ".$basicqualific. 
" 
Post Qualification: ".$postqualific. 
" 
Resume Headline: ".$resumeheadline. 
" 
Experience in years: ".$expyears. 
" 
Experience in months: ".$expmonths. 
" 
Current Salary:".$currsalary. 
" 
Expected Salary: ".$expsalary. 
" 
Current Employer:".$curremploy. 
" 
Job Title: ".$jobtitle. 
" 

Preffered Location: ".$preflocation; 

$headers = "From:" ."xxxxx - " . $from; 

mail($to,$subject,$message,$headers); 



if (!mysqli_query($con, $sql)) 

{ 

echo " Sorry for the inconvenience, please insert again. Error: ".mysqli_error($con); 

} else { 

    echo "Thank you for showing your interest in us. A member of our team will contact you shortly. "; 

} 
?>` 
+0

내가 태그 한 phpmailer를하지만 당신은 실제로 그것을 사용하고 있습니까? 이것이 올바른 사용법이라고 생각하지 않습니다. https://github.com/Synchro/PHPMailer – dcclassics

+0

또한 목표 데이터베이스에 직접 삽입하기 전에 $ _POST 변수에 액세스하여 필터링/위생 처리가 아닌 주입 공격을 시도하고 있습니다. 참조 : http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – VikingBlooded

답변

0

나는 인기 PHPMailer 클래스 추천 :

$mailer = new PHPMailer(); 
$mailer->From = "[email protected]"; 
$mailer->FromName = "John Doe"; 
$mailer->AddReplyTo("[email protected]", "John Doe"); 
$mailer->AddAddress("[email protected]"); 
$mailer->AddAttachment($path_to_file, $filename_visible_to_recipient);   
$mailer->Body = "Hello world";  

if($mailer->Send()) { 
    // success 
}else{ 
    // something wrong happened 
    echo $mailer->ErrorInfo; 
} 
관련 문제