【问题标题】:php send multiple attachments with emailphp 用电子邮件发送多个附件
【发布时间】:2010-06-20 13:05:43
【问题描述】:

嗨,我需要帮助来获取发送附件下面的脚本,我对 php 还很陌生,似乎无法让附件正常工作,电子邮件确实发送了,但我认为标题已损坏并返回垃圾而不是附件

$to      = $_SESSION['companymail']; 

  $email   = $_POST['email']; 
  $name    = $_POST['fullname'];
  $subject = $subject; 
  $comment = $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['fileatt']['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];

         }

     }



/***************************************************************

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);

【问题讨论】:

    标签: php html-email


    【解决方案1】:

    我不建议重新发明轮子,尝试使用具有很高声誉的免费课程。

    查看PHPMailer

    require_once('../class.phpmailer.php');
    
    $mail             = new PHPMailer(); // defaults to using php "mail()"
    
    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);
    
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    
    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");
    
    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";
    
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
    $mail->MsgHTML($body);
    
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
    

    【讨论】:

    • 感谢您的回答,如何使用 $mail->AddAttachment("images/phpmailer.gif"); // 附件 $mail->AddAttachment("images/phpmailer_mini.gif");通过直接从表单帖子中获取文件详细信息,在我的情况下,我想处理多个附件
    • 只需使用来自 $_FILES 的输入,例如 $mail-&gt;AddAttachment($_FILES["file1"]["tmp_name"],$_FILES["file1"]["name"])
    【解决方案2】:

    请看下面的链接,希望这对你有用。

    http://kanakvaghela.wordpress.com/2011/05/21/sending-email-with-multiple-attachments-with-php/

    一切顺利。

    卡纳克·瓦盖拉

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      相关资源
      最近更新 更多