2016-08-02 127 views
1

我的下面的代码有问题吗?如何添加图像作为php HTML邮件的附件?

我想添加一个图像作为HTML邮件的附件从我的表单发送它。用户可以选择添加图像,点击提交按钮将会上传并发送。

但是,点击提交时发生错误,在几秒钟内它会自动重定向到成功页面,但没有电子邮件从表单发送。如果我的代码中有任何错误,有人可以清除代码并指出我所犯的错误?

感谢

require_once "Mail.php"; 
     $semi_rand = md5(time()); 
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
     $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
     $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
     $message .= "--{$mime_boundary}\n"; 
     // preparing attachments    
      $file = fopen($filename,"rb"); 
      $data = fread($file,filesize($filename)); 
      fclose($file); 
      $data = chunk_split(base64_encode($data)); 
      $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" . 
      "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      $message .= "--{$mime_boundary}--\n"; 
$Subject = Trim(stripslashes($_POST['Subject'])); 
$Name = Trim(stripslashes($_POST['Name'])); 
$filename = "/uploads/" . $_FILES["file"]["name"]; 
$Email = Trim(stripslashes($_POST['Email'])); 
$EmailMe = '[email protected]'; 
$ip = $_SERVER['REMOTE_ADDR']; 
$filename="/uploads/file.jpeg"; 
$fname="file.jpeg"; 
$Messages = Trim(stripslashes($_POST['Message'])); 
$smtp = Mail::factory('smtp', array(
     'host' => 'host', 
     'port' => '##', 
     'auth' => true, 
     'username' => '[email protected]', 
     'password' => '*********', 
    )); 
$Body = "<html> html email</html>"; 
$Body1 = "<html> html email</html>"; 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
$headers .= "From: Roberta <[email protected]> \r\n"; 
$headers .= "Reply-To: <[email protected]> \r\n"; 
$headers .= "Return-Path: [email protected]"; 
$headers .= "X-Mailer: PHP \r\n"; 
     $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
     $message .= "--{$mime_boundary}\n"; 
     // preparing attachments    
      $file = fopen($filename,"rb"); 
      $data = fread($file,filesize($filename)); 
      fclose($file); 
      $data = chunk_split(base64_encode($data)); 
      $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" . 
      "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      $message .= "--{$mime_boundary}--\n"; 

$success = mail($Email, $Subject, $message, $headers, "-f " . $from); 
      mail($EmailMe, $Subject, $message, $ip, $headers, "-f " . $from); 

      if (success){ 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=/thanks\">"; 
} 
else{ 
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=/error\">"; 
} 
+1

不要费事去这个做自己,这真是毫无意义,而当前的代码是不安全的。 [使用PHPMailer](https://github.com/PHPMailer/PHPMailer),因为你已经用它标记了这个问题,并且将代码基于提供的示例完成。它更短,更简单,更可靠。 – Synchro

回答

0

我认为它能够更好地使用PHPMailer

OT withot看看这个例子从php.net

有differenece身体,电子邮件的标题(附件(LINUX ,WIN)和(Yahoo Mail,Hotmail,Gmail,...)

<?php 
$to  = $_POST['to']; 
$email = $_POST['email']; 
$name = $_POST['name']; 
$subject = $_POST['subject']; 
$comment = $_POST['message']; 

$To   = strip_tags($to); 
$TextMessage =strip_tags(nl2br($comment),"<br>"); 
$HTMLMessage =nl2br($comment); 
$FromName =strip_tags($name); 
$FromEmail =strip_tags($email); 
$Subject  =strip_tags($subject); 

$boundary1 =rand(0,9)."-" 
.rand(10000000000,9999999999)."-" 
.rand(10000000000,9999999999)."=:" 
.rand(10000,99999); 
$boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-" 
.rand(10000000000,9999999999)."=:" 
.rand(10000,99999); 


for($i=0; $i < count($_FILES['youfile']['name']); $i++){ 
if(is_uploaded_file($_FILES['fileatt']['tmp_name'][$i]) && 
    !empty($_FILES['fileatt']['size'][$i]) && 
    !empty($_FILES['fileatt']['name'][$i])){ 

$attach  ='yes'; 
$end   =''; 

    $handle  =fopen($_FILES['fileatt']['tmp_name'][$i], 'rb'); 
    $f_contents =fread($handle, $_FILES['fileatt']['size'][$i]); 
    $attachment[]=chunk_split(base64_encode($f_contents)); 
    fclose($handle); 

$ftype[]  =$_FILES['fileatt']['type'][$i]; 
$fname[]  =$_FILES['fileatt']['name'][$i]; 
} 
} 

/*************************************************************** 
Creating Email: Headers, BODY 
1- HTML Email WIthout Attachment!! <<-------- H T M L --------- 
***************************************************************/ 
#---->Headers Part 
$Headers  =<<<AKAM 
From: $FromName <$FromEmail> 
Reply-To: $FromEmail 
MIME-Version: 1.0 
Content-Type: multipart/alternative; 
    boundary="$boundary1" 
AKAM; 

#---->BODY Part 
$Body  =<<<AKAM 
MIME-Version: 1.0 
Content-Type: multipart/alternative; 
    boundary="$boundary1" 

This is a multi-part message in MIME format. 

--$boundary1 
Content-Type: text/plain; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$TextMessage 
--$boundary1 
Content-Type: text/html; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$HTMLMessage 

--$boundary1-- 
AKAM; 

/*************************************************************** 
2- HTML Email WIth Multiple Attachment <<----- Attachment ------ 
***************************************************************/ 

if($attach=='yes') { 

$attachments=''; 
$Headers  =<<<AKAM 
From: $FromName <$FromEmail> 
Reply-To: $FromEmail 
MIME-Version: 1.0 
Content-Type: multipart/mixed; 
    boundary="$boundary1" 
AKAM; 

for($j=0;$j<count($ftype); $j++){ 
$attachments.=<<<ATTA 
--$boundary1 
Content-Type: $ftype[$j]; 
    name="$fname[$i]" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
    filename="$fname[$j]" 

$attachment[$j] 

ATTA; 
} 

$Body  =<<<AKAM 
This is a multi-part message in MIME format. 

--$boundary1 
Content-Type: multipart/alternative; 
    boundary="$boundary2" 

--$boundary2 
Content-Type: text/plain; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$TextMessage 
--$boundary2 
Content-Type: text/html; 
    charset="windows-1256" 
Content-Transfer-Encoding: quoted-printable 

$HTMLMessage 

--$boundary2-- 

$attachments 
--$boundary1-- 
AKAM; 
} 

/*************************************************************** 
Sending Email 
***************************************************************/ 
$ok=mail($To, $Subject, $Body, $Headers); 
echo $ok?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>"; 
?> 
+0

是与PHPmailer或没有这些PHP? – Shamshid

+0

代码,无需进行PHPMailer的 的看看它是如何工作只是访问它的链接有清晰的例子 https://github.com/PHPMailer/PHPMailer $ MAIL-> addAttachment('/ var/tmp中/文件.tar 。广州'); //添加附件 $ mail-> addAttachment('/ tmp/image.jpg','new.jpg'); //可选名称 –

相关问题