如何通过PHP将文件附加到邮件

时间:2014-06-05 14:23:32

标签: php mysql email post

我编写了一个php代码,它将数据保存到数据库中,并且还通过用户附加文件(如resume)向客户端发送电子邮件,下面是将数据保存到数据库中的代码,也是将表单字段通过电子邮件发送给客户端的代码现在任何人都可以帮我在发送邮件时附上文件。

<?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 = "xxxxxr@gmail.com";

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

 $from = "website@xxxx.in";

 $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. ";

 }
 ?>`

1 个答案:

答案 0 :(得分:0)

我推荐热门的PHPMailer课程:

$mailer = new PHPMailer();
$mailer->From = "john@doe.com";
$mailer->FromName = "John Doe";
$mailer->AddReplyTo("john@doe.com", "John Doe");
$mailer->AddAddress("recipient@gmail.com");
$mailer->AddAttachment($path_to_file, $filename_visible_to_recipient);        
$mailer->Body = "Hello world";     

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