duanla8800 2016-06-17 12:28
浏览 37

无法使用PHP在电子邮件中发送附件文件

Using this code to send an attached file in email using PHP.

As a result, I am only getting my email message, but I am unable to receive it with an attached file.

Here is my PHP code:

<?php
if($_POST && isset($_FILES['attachment'])){   
$from_email = 'someone@gmail.com'; //sender email
$recipient_email = 'me@gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body 

$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['temp_name'];
$file_type = $_FILES['attachment']['type'];
$file_size = $_FILES['attachment']['size'];

//get base ext of file

$base = basename($file_name);
$extention = substr($base, strlen($base)-4, strlen($base));

//only these files type will allowed
$allowed_extentions = array(".doc", "docx", ".pdf", ".zip", ".png", ".xlsx");



/* check that file type is allowed  */ 

     if(in_array($extention, $allowed_extentions )) {

$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

if($file_error>0)
{
    die('upload error');
}



/* things you need  */

$file = $temp_name ;
$content = chunk_split(base64_encode( file_get_contents($file) )) ;
$uid = md5(uniqid(time()));



    //header
    $header .= "MIME-Version: 1.0
"; 
    $header .= "From:".$from_email."
"; 
    $header .= "Reply-To: ".$user_email."" . "
";
    $header .= "Content-Type: multipart/mixed; boundary = $boundary

"; 

    /* declaring we  have multiple kinds of email */ 
    $header .= "Content-Type: multipart/mixed; boundary=\"" .$uid."\"

";
    $header .= "This is multipart message in MIME formate.
";             

    /* plain text part */ 
    $header .= "--".$uid."
";
    $header .= "Content-Type:text/plain; charset=iso-8859-1
";
    $header .= "Content-Transfer-Encoding : 7bit

";
    $header .= $message."

";

    /* file attachment  */ 
    $header .= "--".$uid."
";
    $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"
";
    $header .= "ContentTransfer-Encoding:base64
";
    $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"
";
    $header .= $content."

";

    require_once "Mail.php";

 /* mail setup recipients, subject etc */
 $name =  'sanju';       //$_POST['name'];
 $email =  $from_email ;     //         $_POST['email'];
 $subject = 'Test mail';   //$_POST['subject'];
 //$comments = 'This is body of the message';   //$_POST['message'];

  $recipients = 'me@gmail.com';

$header = array (
           'From' => "someone@gmail.com",
           'Content-type' => "text/html;charset=UTF-8",
           'Reply-To' => "someone@gmail.com",
           'To' => "contact@domain.com",

           );



 $mailmsg="Name : $name 

 Email :$email  

 Message : $message   ";
 /* SMTP server name, port, user/passwd */
 $smtpinfo["host"] = "ssl://lnxind3.cloudhostdns.net";
 $smtpinfo["port"] = "465";
 $smtpinfo["auth"] = true;
 $smtpinfo["username"] = "contact@net-lytics.com";
 $smtpinfo["password"] = "sanjeev12@";
 /* Create the mail object using the Mail::factory method */
 $mail_object =& Mail::factory("smtp", $smtpinfo);
 /* Ok send mail */
 echo $mail_object->send($recipients, $header, $mailmsg);
 $msg = " Thank you for contacting us. .";;




 } else {
            echo " File type not allowed";
        }
}
?>

The HTML form:

<form name="form1" enctype="multipart/form-data" method="post" action="">
<label>Your Name
<input type="text" name="name" />
</label> 
<label>Your Email
<input type="email" name="email" />
</label> 
<label>Attachment
<input type="file" name="attachment" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 目详情-五一模拟赛详情页
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line