Very similar to the previous post on the same subject, but this time put into a ‘class’ file for neatness..
I have just used this on the new www.lady-drivers.co.uk site for sending Vouchers by email after subscribing.
content");
// $mailer->attachFile("a.pdf");
// $mailer->attachFile("b.pdf");
// $mailer->send() ? "Success": "Ooops, an Error occurred!";
/*
* send emails with pdf attachment
*/
class AttachMailer{
private $from, $to, $subject, $mess, $hash, $output;
private $documents = Array();
/*
* @params from: sender (email format)
* to : recipient (email format)
* subject : Subject line
* mess : HTML formatted message
*/
function __construct($_from, $_to, $_subject, $_mess){
$this->from = $_from;
$this->to = $_to;
$this->subject = $_subject;
$this->mess = $_mess;
$this->hash = md5(date('r', time()));
}
/*
* @params url : document path
*/
public function attachFile($url, $name = ""){
$attachment = chunk_split(base64_encode(file_get_contents($url)));
$docName = $name == "" ? basename($url) : $name;
$randomHash = $this->hash;
$docOutput = "--PHP-alt-$randomHash--\r\n\r\n"
."--PHP-mixed-$randomHash\r\n"
."Content-Type: application/pdf; name=\"$docName\" \r\n"
."Content-Transfer-Encoding: base64 \r\n"
."Content-Disposition: attachment \r\n\r\n"
.$attachment . "\r\n";
$this->documents[] = $docOutput;
}
private function makeMessage(){
$randomHash = $this->hash;
$messageOutput = "--PHP-mixed-$randomHash\r\n"
."Content-Type: multipart/alternative; boundary=PHP-alt-$randomHash\r\n\r\n"
."--PHP-alt-$randomHash\r\n"
."Content-Type: text/plain; charset='iso-8859-1'\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$this->mess . "\r\n\r\n"
."--PHP-alt-$randomHash\r\n"
."Content-Type: text/html; charset='iso-8859-1'\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
. $this->mess . "\r\n";
foreach($this->documents as $document){
$messageOutput .= $document;
}
$messageOutput .="--PHP-mixed-$randomHash;--";
$this->output = $messageOutput;
}
public function send(){
$this->makeMessage();
$from = $this->from;
$randomHash = $this->hash;
$headers = "From: $from\r\nReply-To: $from";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-$randomHash\"";
$mail_sent = @mail( $this->to, $this->subject, $this->output, $headers );
return $mail_sent ? true : false;
}
}