0

1.这里我正在尝试使用 php mail() 函数将我的 jpg 图像文件作为附件发送到邮件。

2.我的邮件附件与word文档(.doc作为附件)一起发送很好。但是,当我尝试将.jpg作为附件发送时抛出错误。我使用的代码:

<?php
/*If there is no error, send the email*/
if(isset($_POST['submit-button-name'])) {       
  $uname=$_POST['uname'];                           
  $to = $_POST['mailid'];
  $mobileno=$_POST['mobile'];
  $location=$_POST['location'];
  $from = "Urname <urname@domainname.com>";
  $subject = $_POST['uname']." testing";
  $separator = md5(time());
  /* carriage return type (we use a PHP end of line constant)*/
    $eol = PHP_EOL;
  /*attachment name*/
    $filename = "image.jpg";
    $attachment = chunk_split(base64_encode(file_get_contents('image.jpg')));
  /*main header*/
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
  /*no more headers after this, we start the body!*/
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "Check out the attachment.".$eol;
 /*message*/
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;
 /*attachment*/
    $body .= "--".$separator.$eol;
$body .= "Content-Type: image/jpg; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment; file=\"".$filename."\"".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
 /*send message*/
if (mail($to, $subject, $body, $headers)) {
    $mail_sent=true;
    echo "<font color='white'>Mail sent</font>";
    $frm="urname@domainname.com";
    $subj="Acknowledgement mail for Brochure form";
    $msg = $_POST['username'] . ",\n\nThank you for your recent enquiry.";
    $body = "Name: ".$_POST['uname'] ."\n\nEmail: ".$_POST['mailid'] ."\n\nMobile: 
        ".$_POST['mobile'] ."\n\nLocation: ".$_POST['location'];
    $headers = 'From: '.'careers@domainname.com';
    mail($frm,$subj,$body,$headers);
} else {
    $mail_sent=false;
}
if($mail_sent == true)
{
/*to clear the post values*/
  $uname="";
  $to="";
  $mobileno="";
  $location="";
}
else{
  echo "Error,Mail not sent";
}
    }
    ?>

3.错误被提出为:

警告:file_get_contents(image.jpg) [function.file-get-contents]:无法
打开流:第 48 行的 footer.php 中没有此类文件或目录

4.我曾经收到带有正文内容的邮件(“检查附件”),附件为image.jpg,0k(没有打开图像)。

5.please help.thanks提前。它的紧急。

4

3 回答 3

0

我认为内容类型是 jpeg 而不是 jpg。

Content-Type: image/jpeg;

在您添加附件的部分中。

还有以下几行:

$body .= "Content-Disposition: attachment; file=\"".$filename."\"".$eol.$eol;

$body .= "--".$separator."--";

大概应该是:

$body .= "Content-Disposition: attachment;".$eol.$eol;

$body .= "--".$separator."--".$eol;

于 2012-07-07T06:31:34.350 回答
0

通过给出我的绝对路径来解决问题 - file_get_contents('absolute path'); 我用 $_SERVER['DOCUMENT_ROOT'].'filename'; 它成功了。另外,我将图像/jpg 更改为图像/jpeg。谢谢大家。

于 2012-07-10T06:43:26.527 回答
0

我通过在这行代码中提供图像的完整绝对路径解决了我的问题:

$attachment = chunk_split(base64_encode(file_get_contents('http://wordpress1/wp-content/themes/MyTheme/VyTCDC-brochure.jpg')));

所以,你可以走你的完整路径。现在,它就像一个魅力。

于 2012-08-02T05:26:36.477 回答