Sending Mail with file stored in Database as atachment

the mail() function

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 23
Joined: Fri Sep 30, 2011 1:26 pm

Thu Oct 13, 2011 4:24 am


I'm trying send an email with an attachment which is stored in db.
The way it works: the admin can upload files to database, and ther's a page,
where he can view all the files in database. under each file ther's a link "Send",
when the admin clicks on this link, the link grabs the "id" of the specific file,
and then redirects to the "send email" page, where there's a form to add message.
It's giving me the following fatal error:

Fatal error: Call to a member function addAttachment() on a non-object in /home/acfango1/public_html/admin/send-newsletter.php on line 58

here is my code:

Code: Select all

//this is the send-email.php page

if("Send Newsletter" == $_POST['submit'])

    $message = FilterData($_POST['message']);
    $get_emails = new Newsletter();
    $emails = $get_emails->emails;
   $get_file = new Newsletter();
   $file = $get_file->GetImageName($_id);

    //send email
            $headers = "MIME-Version: 1.0\r\n";
            $headers.="Content-type: text/html; charset=iso-8859-1\r\n";
            $from = "";
            $to = $all_emails;
             $subject = "Newsletter";
             $message ->addAttachment($file);

             if(mail($to, $subject, $message,$from))

                 $warning = "The newsletter was sent successfully!";
                 $warning = "Something went wrong! Newsletter could not be sent.";

I think the problem might be in my GetImageName() function inside the class.

here it is:

Code: Select all

//this is the Class file

 public function GetImageName($id)

            $sql = "SELECT upload as file FROM newsletter WHERE id='{$id}'" ;
            $result = $this->db->mysqli->query($sql);

                     throw new Exception("Query failed: " . $sql . " - " . $this->db->mysqli->error);

                    $file_name = $result->fetch_assoc();
                    return $file_name['file'];

            } catch(Exception $e){
               echo("Message: " . $e->getMessage());


Although it looks perfect for me, but being a young php dev, and not being familiar with addAtachment() function, I dont know if i have to include the full path of of my file.

Any help would be much appreciated
thanks in advance,
Mike Spider

Post Reply