File: /var/www/vhosts/paiskincare.com/httpdocs2/sisterweek/emailfunctions.php
<?php
function mailpart($boundary, $type, $transfer, $disposition, $content, $id=NULL) {
// Start chunk with boundary
$chunk="--".$boundary."\n";
$chunk.="Content-Type: $type\n";
$chunk.="Content-Transfer-Encoding: $transfer\n";
if (isset($disposition)) $chunk.="Content-Disposition: $disposition\n";
if (isset($id)) $chunk.="Content-Id: <$id>\n";
// End headers
$chunk.="\n";
$chunk.=$content."\n\n";
return $chunk;
}
function sendemail($smtp,$addresses,$subject,$text,$html,$attachments,$from=null,$headers=null) {
// Build message
// boundary
$messageboundary="messageboundary-".md5(microtime());
// Text
$message=mailpart($messageboundary, "text/plain; charset=utf-8", "8bit", NULL, $text);
// HTML
$message.=mailpart($messageboundary, "text/html; charset=utf-8", "8bit", NULL, $html);
// multipart end boundary
// must begin and end in two hyphens
$message.="--".$messageboundary."--";
$messagetype="multipart/alternative; boundary=\"$messageboundary\"";
$messagetransfer="8bit";
// Add attachments
$attachments=$attachments;
if (count($attachments)==0) {
// No attachments, so use $message as entire email
$mailtype=$messagetype;
$mailtransfer=$messagetransfer;
$mailbody=$message;
} else {
// Nest message within multipart/mixed structure containing attchments
// NB use different boundary
$mailboundary="mailboundary-".md5(microtime());
$mailtype="multipart/mixed; boundary=\"$mailboundary\"";
$mailtransfer="8bit";
// add message
$mailbody=mailpart($mailboundary, $messagetype, $messagetransfer, NULL, $message);
foreach ($attachments AS $attachment) {
// type and name should probably be escaped somehow
$data=$attachment['data'];
if (strlen($data)==0) $data=file_get_contents($attachment['path']);
if (strlen($data)==0) continue;
// add content
$mailbody.=mailpart($mailboundary,
$attachment['type']."; name=\"".$attachment['name']."\"", // content type and file name
"base64", // transfer encoding
"attachment", // disposition
chunk_split(base64_encode($data),76,"\n"), // data, ensuring consistent line break character
"attachment".$attachment['attachmentid']
);
}
// multipart end boundary
// must begin and end in two hyphens
$mailbody.="--".$mailboundary."--";
}
// Prepend warning message. Never seen in MIME compatible clients (= all of them)
$mailbody="This is a MIME encoded message.\n\n".$mailbody;
/*
echo "<h1>Source</h1>";
$lines=explode("\n",$mailbody);
foreach ($lines AS $line) {
echo htmlspecialchars($line)."<br>\n";
}
*/
$to=implode($addresses,", ");
// require_once('dkim.php');
$headers=array(
'From' => "Pai Skincare <".$from.">",
'To' => $to,
'Subject' => $subject,
'MIME-Version' => '1.0',
'Content-Type' => $mailtype,
'Content-Transfer-Encoding' => $mailtransfer
);
// $headers['X-DKIM'] = GetDKIM($to,$headers,$subject,$mailbody);
$mail = $smtp->send($to, $headers, $mailbody);
if (PEAR::isError($mail)) {
return $mail->getMessage();
} else {
return false;
}
}
?>