PHP Mail Class - Send HTML Mail / Attachments

Posted on by Kenny Cason
tags = [ attachments, html, mail, php ]

This is a sample class to send mail using PHP. It also demonstrates how to attach images.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

class Mail {
    var $parts;
    var $to;
    var $cc;
    var $bcc;
    var $from;
    var $headers;
    var $subject;
    var $body;
    var $html;
    var $host;
    var $port;

    function __construct() {
        $this->parts = array();
        $this->to = "";
        $this->cc = "";
        $this->bcc = "";
        $this->from = "";
        $this->subject = "";
        $this->body = "";
        $this->headers = "";
        $this->html = false;
    }

    function addAttachment($message,$name = "",$ctype = "application/octet-stream") {
        $this->parts[] = array(
                    "ctype" => $ctype,
                    "message" => $message,
                    "encode" => "base64",
                    "name" => $name
                    );            
    }

    function buildMessage($part) {
        $message = $part["message"];
        $message = chunk_split(base64_encode($message));
        $encoding = "base64";
        return "Content-Type: ".$part["ctype"].($part["name"]? ";name = \"".$part["name"]."\"" : "").
               "\nContent-Transfer-Encoding: $encoding\n\n$message\n";
    }

    function buildMultipart() {
        $boundry = "HKC".md5(uniqid(time()));
        $multipart = "Content-Type: multipart/mixed; boundary =  \"$boundry\"\n\n";
        $multipart .= "This is a MIME encoded message.\n\n--$boundry";
        for($i = sizeof($this->parts)-1; $i >= 0; $i--) {
            $multipart .= "\n".$this->buildMessage($this->parts[$i])."--$boundry";
        }
        return $multipart .= "--\n";
   }

    function getMail($complete = true) {
        $mime = "";
        if(!empty($this->from)) {
            $mime .= "From: ".$this->from."\n";
        }
        if(!empty($this->headers)) {
            $mime .= $this->headers."\n";
        }
        if($complete) {
            if(!empty($this->cc)) {
                $mime .= "Cc: ".$this->cc."\n";
            }
            if(!empty($this->bcc)) {
                $mime .= "Bcc: ".$this->bcc."\n";
            }
            if(!empty($this->subject)) {
                $mime .= "Subject: ".$this->subject."\n";
            }
        }     
        if(!empty($this->body)) {
            $this->addAttachment($this->body,"",($this->html? "text/html":"text/plain"));
        }
        $mime .=  "MIME-Version: 1.0\n".$this->buildMultipart();
        return $mime;
    }

    function send() {
        if(!empty($this->cc)) {
            $mime = $this->getMail(true);
        } else {
            $mime = $this->getMail(false);
        }
        if(!empty($this->host)) {
             ini_set("SMTP",$this->host);
        }
        return mail($this->to,$this->subject,$this->body,$mime);
    }
}

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$fp = fopen("somepicture.jpg","r");
$data = fread($fp,filesize("somepicture.jpg"));
fclose($fp);

$mail = new Mail();
$mail->from = "me@somewhere.com";
$mail->to = "you@somewhere.com";
$mail->subject = "welcome";
$mail->body = "<b>How's it going?</b>";
$mail->html = true;
$mail->addAttachment($data,"somepicture.jpg" ,"image/jpeg" );
$mail->send();
comments powered by Disqus