-
Notifications
You must be signed in to change notification settings - Fork 1
/
Email.php
66 lines (56 loc) · 1.89 KB
/
Email.php
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
<?php
require_once "Mail.php";
require_once "Mail/mime.php";
error_reporting(E_ERROR | E_PARSE);
class Email
{
public $to = '';
public $subject = '';
public $html = '';
public $from = '';
public $images = [];
public $attachments = [];
private $smtp_host = 'smtp.gmail.com';
private $smpt_username = 'fellowship@makeadiff.in';
private $smtp_password = 'fellowshipgonemad';
function send() {
$headers = [
'From' => $this->from,
'To' => $this->to,
'Subject' => $this->subject
];
$mime = new Mail_mime(array('eol' => "\n"));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$index = 0;
foreach($this->images as $key => $image) {
if($image and file_exists($image)) {
$mime->addHTMLImage($image, finfo_file($finfo, $image));
$cid = $mime->_html_images[$index]['cid'];
$this->html = str_replace("%CID-$key%", $cid, $this->html);
// print "Replaced '%CID-$key%' with '$cid' - $index\n";
$index++;
}
}
$mime->setHTMLBody($this->html);
foreach($this->attachments as $attachment) {
if($attachment and file_exists($attachment)) {
$mime->addAttachment($attachment, finfo_file($finfo, $attachment));
}
}
$smtp = Mail::factory('smtp', [
'host' => $this->smtp_host,
'auth' => true,
'username' => $this->smpt_username,
'password' => $this->smtp_password
]);
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = $smtp->send($this->to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
return false;
}
// echo "Sent successfully";
return true;
}
}