2013-07-11 97 views
0

我已经使用此代码在PHP邮件发送附件。我正在使用带有wamp的Apache http服务器。用附件发送电子邮件在php中

  <?php $to = '[email protected]'; 

// Declaring the necessary variables for mail sending : 

       $subject = 'Testing sendmail.exe'; 
       $name = $_REQUEST['name'] ; 
       $email = $_REQUEST['email'] ; 
       $mob = $_REQUEST['mob'] ; 
       $msg = $_REQUEST['msg'] ; 
       $message = ' santosh enquire \n\nName: '.$name.' \n Email:'. $email.' \n Mobile:'.  $mob.' \n Message: '.$msg.' \n '; 

// Declaration of the attributes for attachment. 

       $upload_name=$_FILES["upload"]["name"]; 
       $upload_type=$_FILES["upload"]["type"]; 
       $upload_size=$_FILES["upload"]["size"]; 
       $upload_temp=$_FILES["upload"]["tmp_name"]; 
       $fp = fopen($upload_temp, "rb"); 
       $file = fread($fp, $upload_size); 
       fclose($fp); 
       $file = chunk_split(base64_encode($file)); 
       $num = md5(time()); 

// Defining the contents of the header. 

       $headers = "From: ".$email. "\r\n" . "CC: [email protected]" ; 

       $headers .= "MIME-Version: 1.0\r\n"; 
       $headers .= "Content-Type: multipart/mixed; "; 
       $headers .= "boundary=".$num."\r\n"; 
       $headers .= "--$num\r\n"; 

       //$headers .= "Message-ID: <".gettimeofday()." [email protected]".$_SERVER['SERVER_NAME'].">\r\n"; 
       $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; 

       $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
       $headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
       $headers .= "".$message."\n"; 
       $headers .= "--".$num."\n"; 

       $headers .= "Content-Type:".$upload_type." "; 
       $headers .= "name=\"".$upload_name."\"r\n"; 
       $headers .= "Content-Transfer-Encoding: base64\r\n"; 
       $headers .= "Content-Disposition: attachment; "; 
       $headers .= "filename=\"".$upload_name."\"\r\n\n"; 
       $headers .= "".$file."\r\n"; 
       $headers .= "--".$num."--"; 
       //$data = chunk_split(base64_encode($data)); 

// sending the mail. 

       if(mail($to, $subject, $message, $headers)) 
       { 
       echo "Email sent"; 
       send(); 

       } 
       else 
       { 
       echo "Email sending failed"; 
       } 
    //send mail in client 
       function send() 
       { 
       $email = $_REQUEST['email'] ; 
       $name = $_REQUEST['name'] ; 
       $to= trim($email); 
       $subject = 'thanx'; 
       $message = '<html><head><title>HTML email</title></head> 
       <body style="background-color:#000099;"><p style="color:#000099;">This email contains HTML Tags!</p><table><tr><th>Firstname</th><th>Lastname</th></tr><tr><td>John</td><td>Doe</td></tr></table></body></html>';$headers1 = 'MIME-Version: 1.0' . "\r\n"; 
       $headers1.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
       $headers1.= "From: ".$email; 

       if(mail($to, $subject, $message,$headers1)) 
       { 
       echo "Email sent"; 
       } 
       else 
       { 
       echo "Email sending failed"; 
       echo $to; 
       echo $subject; 
       echo $message; 
       } 
      } 

      ?> 

但我得到以下错误;

Delivery to the following recipient failed permanently: 

       [email protected] 

Technical details of permanent failure: 
DNS Error: Domain name not found. 

请帮帮我。 Thnx提前。

回答

0
$mail = new PHPMailer; 

$mail->IsSMTP();          // Set mailer to use SMTP 
$mail->Host = ''; // Specify main and backup server 
$mail->Port = ''; 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '';       // SMTP username 
$mail->Password = '';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable encryption, 'ssl' also accepted 

$mail->From = ''; 
$mail->FromName = ''; 
$mail->AddAddress($row['client_email'], ''); // Add a recipient 

$mail->AddReplyTo('', 'Information'); 
$mail->AddCC('[email protected]'); 
$mail->AddBCC('[email protected]'); 

$mail->WordWrap = 50;  
          // Set word wrap to 50 characters 

$mail->AddAttachment('kurtacompany/techreporting/upload/"'.$row['file1'].'"'); // Add attachments 
$mail->IsHTML(true);         // Set email format to HTML 

$mail->Subject = ''; 
$mail->Body = '<p>type whatever you want</p>'; 

if(!$mail->Send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit; 
} 

echo 'Message has been sent'; 
+0

ü可以给我PHPMailer的代码。感谢名单 – user2572266