如何将图像作为附件添加到 php HTML 邮件?

How to add images as an attachment to php HTML mail?

我下面的代码有问题吗?

我正在尝试将图像作为附件添加到 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 = 'shmshd12@gmail.com'; 
$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' => 'mail@example.com',
        '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 <request@robertalollobrigida.ga> \r\n";
$headers .= "Reply-To: < reply@example.com > \r\n";
$headers .= "Return-Path: user@example.com";
$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\">";
}

我觉得最好用PHPMailer

或不看 php.net

中的示例

There differenece in body, headers of email (with attachment, without attachment), see this complete example below: work great for me (LINUX , WIN) and (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>";
?>