PHP email w/ attachments help needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
digitalprisoner
Forum Newbie
Posts: 2
Joined: Thu Aug 05, 2004 8:07 pm

PHP email w/ attachments help needed

Post by digitalprisoner »

Okay any help would be greatly appreciated! I have an HTML form, mail.php and class.mail.php. I have gotten the attachment part to work, but pastes itself in the body of the email message. I want to be able to allow multiple attachments to be selected and for them not to show up, posted in the message.

Code: Select all

<?php
  include('class.Email.php');

  $Sender = $_POST['sender'];
  $Recipiant = "some@one.com";
  $Cc = $_POST['carbon'];
  $Bcc = $_POST['blindcarbon'];
  $attachedfile = $_POST['attachments'];

  $textVersion = $_POST['textversion'];
  $htmlVersion = "<font face='verdana' color='blue'><b>Hi</b></font>";

  unset($msg);

  $msg = new Email($Recipiant, $Sender, "A Test HTML Email!");
  $msg->Cc = $Cc;
  $msg->Bcc = $Bcc;

  $msg->TextOnly = false;
  $msg->Content = $htmlVersion;

  $msg->Attach($attachments, "text/plain");

  $SendSuccess = $msg->Send();

  echo "HTML email w/attachment was ",
       ($SendSuccess ? "sent" : "not sent"), "<br>";

  unset($msg);

?>

Code: Select all

<?php
if(isset($GLOBALS["emailmsgclass_php"])) { return; }  //** onlyinclude once.
  $GLOBALS["emailmsgclass_php"] = 1;                    //** file was included.

  define("EmailNewLine", "\r\n");

  define("EmailXMailer", "PHP-EMAIL,v1.1 (William Fowler)");

  define("DefaultCharset", "iso-8859-1");

class Email
{
  var $To = null;

  var $Cc = null;

  var $Bcc = null;

  var $From = null;

  var $Subject = null;

  var $Content = null;

  var $Attachments;

  var $Headers = null;

  var $TextOnly = true;

  var $Charset = null;

  function Email($to=null, $from=null, $subject=null, $headers=null)
  {
    $this->To = $to;
    $this->From = $from;
    $this->Subject = $subject;
    $this->Headers = $headers;

    $this->Attachments = Array();
    $this->Attachments["text"] = null;
    $this->Attachments["html"] = null;
  }

  function SetMultipartAlternative($text=null, $html=null)
  {

    if(strlen(trim(strval($html))) == 0 || strlen(trim(strval($text))) == 0)
      return false;
    else
    {

      $this->Attachments["text"] = new EmailAttachment(null, "text/plain");
      $this->Attachments["text"]->LiteralContent = strval($text);

      $this->Attachments["html"] = new EmailAttachment(null, "text/html");
      $this->Attachments["html"]->LiteralContent = strval($html);

      return true;  //** operation was successful.
    }
  }

  function Attach($pathtofile, $mimetype=null)
  {

    $attachment = new EmailAttachment($pathtofile, $mimetype);
    if(!$attachment->Exists())
      return false;
    else
    {
      $this->Attachments[] = $attachment;  //** add the attachment to list.
      return true;                         //** attachment successfully added.
    }
  }

  function IsComplete()
  {
    return (strlen(trim($this->To)) > 0 && strlen(trim($this->From)) > 0);
  }

  function Send()
  {
    if(!$this->IsComplete())  //** message is not ready to send.
      return false;           //** no message will be sent.

    $theboundary = "-----" . md5(uniqid("EMAIL"));

    $headers = "Date: " . date("r", time()) . EmailNewLine .
               "From: $this->From" . EmailNewLine;

    if(strlen(trim(strval($this->Cc))) > 0)
      $headers .= "CC: $this->Cc" . EmailNewLine;

    if(strlen(trim(strval($this->Bcc))) > 0)
      $headers .= "BCC: $this->Bcc" . EmailNewLine;

    if($this->Headers != null && strlen(trim($this->Headers)) > 0)
      $headers .= $this->Headers . EmailNewLine;


    $isMultipartAlternative = ($this->Attachments["text"] != null &&
                               $this->Attachments["html"] != null);


    $baseContentType = "multipart/" . ($isMultipartAlternative ?
                                       "alternative" : "mixed");

    $headers .= "X-Mailer: " . EmailXMailer . EmailNewLine .
                "MIME-Version: 1.0" . EmailNewLine .
                "Content-Type: $baseContentType; " .
                "boundary="$theboundary"" . EmailNewLine . EmailNewLine;

    if($isMultipartAlternative)
    {

      $thebody = "--$theboundary" . EmailNewLine .
                  $this->Attachments["text"]->ToHeader() . EmailNewLine .
                 "--$theboundary" . EmailNewLine .
                  $this->Attachments["html"]->ToHeader() . EmailNewLine;
    }

    else
    {

      $theemailtype = "text/" . ($this->TextOnly ? "plain" : "html");
      if($this->Charset == null)
        $this->Charset = DefaultCharset;

      $thebody = "--$theboundary" . EmailNewLine .
                 "Content-Type: $theemailtype; charset=$this->Charset" .
                  EmailNewLine . "Content-Transfer-Encoding: 8bit" .
                  EmailNewLine . EmailNewLine . $this->Content .
                  EmailNewLine . EmailNewLine;

      foreach($this->Attachments as $attachment)
      {
 
        if($attachment != null)
        {
          $thebody .= "--$theboundary" . EmailNewLine .
                       $attachment->ToHeader() . EmailNewLine;
        }
      }
    }

    $thebody .= "--$theboundary--";

    return mail($this->To, $this->Subject, $thebody, $headers);
  }
}

class EmailAttachment
{

  var $FilePath = null;

  var $ContentType = null;

  var $LiteralContent = null;

  function EmailAttachment($pathtofile=null, $mimetype=null)
  {

    if($mimetype == null || strlen(trim($mimetype)) == 0)
      $this->ContentType = "application/octet-stream";
    else
      $this->ContentType = $mimetype;

    $this->FilePath = $pathtofile;  //** save the path to the file attachment.
  }

  function HasLiteralContent()
  {
    return (strlen(strval($this->LiteralContent)) > 0);
  }

  function GetContent()
  {

    if($this->HasLiteralContent())
      return $this->LiteralContent;

    else
    {
      if(!$this->Exists())  //** file does not exist.
        return null;        //** no content is available.
      else
      {

        $thefile = fopen($this->FilePath, "rb");
        $data = fread($thefile, filesize($this->FilePath));
        fclose($thefile);
        return $data;
      }
    }
  }

  function Exists()
  {
    if($this->FilePath == null || strlen(trim($this->FilePath)) == 0)
      return false;
    else
      return file_exists($this->FilePath);
  }

  function ToHeader()
  {
    $attachmentData = $this->GetContent();  //** get content for the header.
    if($attachmentData == null)             //** no valid attachment content.
      return null;                          //** no header can be generted.

    $header = "Content-Type: $this->ContentType;";

    if(!$this->HasLiteralContent())
    {
      $header .= " name="" . basename($this->FilePath) . """ . EmailNewLine .
                 "Content-Disposition: attachment; filename="" .
                  basename($this->FilePath) . """;
    }
    $header .= EmailNewLine;

    $header .= "Content-Transfer-Encoding: base64" . EmailNewLine .
                EmailNewLine;

    $header .= chunk_split(base64_encode($attachmentData), 76, EmailNewLine) .
               EmailNewLine;

    return $header;  //** return the headers generated by file.
  }
}

?>

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Mail Form&lt;/title&gt;
&lt;body BACKGROUND=/im/blood.jpg&gt;
&lt;form method="post" action="mail.php"&gt;
&lt;font color="black" face="verdana" size="4"&gt;&lt;b&gt;From: &lt;/b&gt;&lt;/font&gt;&lt;input type="Text" name="sender"&gt;&lt;br&gt;&lt;br&gt;
&lt;font color="black" face="verdana" size="4"&gt;&lt;b&gt;Cc: &lt;/b&gt;&lt;/font&gt;&lt;input type="Text" name="carbon"&gt;&lt;br&gt;&lt;br&gt;
&lt;font color="black" face="verdana" size="4"&gt;&lt;b&gt;Bcc: &lt;/b&gt;&lt;/font&gt;&lt;input type="Text" name="blindcarbon"&gt;&lt;br&gt;&lt;br&gt;
&lt;font color="black" face="verdana" size="4"&gt;&lt;b&gt;Message: &lt;/b&gt;&lt;/font&gt;&lt;TEXTAREA name="textversion" ROWS=20 COLS=60&gt;&lt;/TEXTAREA&gt;&lt;br&gt;&lt;br&gt;
&lt;ACTION="mailto:webzz@nc.rr.com" ENCTYPE="multipart/form-data"&gt;
&lt;FONT size="2" color="white" face="Verdana"&gt;Attachment: &lt;/FONT&gt;&lt;INPUT TYPE="file" NAME="attachments" MAXLENGTH=50 ALLOW="text/*" &gt;&lt;br&gt;&lt;br&gt;
&lt;input type="Submit" name="submit" value="Send Mail"&gt;
&lt;input type="reset" value="Reset"&gt;&lt;/form&gt;
&lt;/HTML&gt;
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

Take a look at:

http://sourceforge.net/projects/phpmailer/

Works fine!
Post Reply