附件上传但如何通过电子邮件发送?

时间:2015-02-17 16:50:37

标签: php

我的PHP代码正常工作,我需要将图像附加到变量上,然后通过电子邮件发送其余的表单。我得到2个错误。有一个错误告诉我第75行的$ messageImage是未定义的吗?另一个是:在第75行的非对象上调用成员函数addAttachment()。

有人可以解释声明变量的正确方法,并确保它是一个对象,以便addAttachment工作。谢谢

 //copy the temp. uploaded file to uploads folder
$upload_folder = 'uploads/';

$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;

$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path)) {
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors = '\n error while copying the uploaded file';
  }
}



$to = "example@gmail.com"; // this is your Email address
$from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); // this is the sender's Email address
$first_name = filter_var($_POST['first_name'], FILTER_SANITIZE_STRING);
$story = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
$subject = "subject";
$subject2 = "Thank You for submitting";

$messageImage->addAttachment($path_of_uploaded_file);


$message = $first_name . " wrote the following:" . "\n\n" . $story;
$message2 = "Here is a copy of your story " . $first_name . "\n\n" . $story;

$headers = "From:" . $from;
$headers2 = "From:" . $to;

mail($to,$subject,$message,$messageImage,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender

2 个答案:

答案 0 :(得分:0)

在您的代码中,未设置$ messageImage。由于它没有设置,它是NULL,导致下一个错误,你试图引用一个对象的方法,但它是null。

要发送带附件的邮件,您需要设置电子邮件标头,并且邮件中的图像是base64编码的。我会看看其他StackOverflow的答案:

https://stackoverflow.com/a/17371739/2832264

制作一个简单的对象:

$obj = new stdClass();

否则,您将创建一个类,确保它包含在您的脚本中,并创建一个新的实例:

$obj = new Person();

我会阅读有关课程的官方PHP文档。

答案 1 :(得分:0)

'$ messageImage'是代码中的一个对象,它没有定义。 如果检查如下,你可以用简单的方法检查这个,

       if ($messageImage) {
            // do something
       }

以下代码可以为您服务,

       //copy the temp. uploaded file to uploads folder
       $upload_folder = 'uploads/';

       $to = "example@gmail.com"; // this is your Email address
       $from = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); // this is the sender's Email address
       $first_name = filter_var($_POST['first_name'], FILTER_SANITIZE_STRING);
       $story = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
       $subject = "subject";
       $subject2 = "Thank You for submitting";

       $message = $first_name . " wrote the following:" . "\n\n" . $story;
       $message2 = "Here is a copy of your story " . $first_name . "\n\n" . $story;

       $headers = "From: $from";
       // boundary
       $semi_rand = md5(time());
       $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

       // headers for attachment
       $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

       // multipart 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";

       $content = '';
       $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
       if($tmp_path) {
                    $filename = $_FILES["uploaded_file"]["name"];
                    $path_of_uploaded_file = $upload_folder . $filename;
                    if(!copy($tmp_path, $path_of_uploaded_file))
                    {
                                  $errors = '\n error while copying the uploaded file';
                    }
                    $file_size = filesize($path_of_uploaded_file);
                    $handle = fopen($path_of_uploaded_file, "rb");
                    $content = fread($handle, $file_size);
                    fclose($handle);
                    $content = chunk_split(base64_encode($content));
             }

       // if attachment
       if ($content) {
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"{$filename}\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
$message .= "--{$mime_boundary}\n";
       }
       $headers2 = "From:" . $to;

       mail($to, $subject, $message, $headers);
       mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender