Add iCal event attachments and test case, fixes #47, thanks to @reblutus

Minor code cleanup
Bundle EasyPeasyICS class in extras
This commit is contained in:
Synchro 2013-05-29 02:15:17 +02:00
parent 7f39eff84b
commit 3c80c56a55
4 changed files with 171 additions and 25 deletions

View File

@ -22,6 +22,7 @@
* Fix cid generation in MsgHTML (Thanks to @digitalthought)
* Fix handling of multiple SMTP servers (Thanks to @NanoCaiordo)
* SMTP->Connect() now supports stream context options (Thanks to @stanislavdavid)
* Add support for iCal event alternatives (Thanks to @reblutus)
## Version 5.2.6 (April 11th 2013)
* Reflect move to PHPMailer GitHub organisation at https://github.com/PHPMailer/PHPMailer

View File

@ -35,7 +35,9 @@
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!\n");
if (version_compare(PHP_VERSION, '5.0.0', '<') ) {
exit("Sorry, PHPMailer will only run on PHP version 5 or greater!\n");
}
/**
* PHP email creation and transport class
@ -91,8 +93,8 @@ class PHPMailer {
public $FromName = 'Root User';
/**
* Sets the Sender email (Return-Path) of the message. If not empty,
* will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
* Sets the Sender email (Return-Path) of the message.
* If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
* @var string
*/
public $Sender = '';
@ -111,21 +113,31 @@ class PHPMailer {
public $Subject = '';
/**
* Sets the Body of the message. This can be either an HTML or text body.
* If HTML then run IsHTML(true).
* An HTML or plain text message body.
* If HTML then call IsHTML(true).
* @var string
*/
public $Body = '';
/**
* Sets the text-only body of the message. This automatically sets the
* email to multipart/alternative. This body can be read by mail
* clients that do not have HTML email capability such as mutt. Clients
* that can read HTML will view the normal Body.
* The plain-text message body.
* This body can be read by mail clients that do not have HTML email
* capability such as mutt & Eudora.
* Clients that can read HTML will view the normal Body.
* @var string
*/
public $AltBody = '';
/**
* An iCal message part body
* Only supported in simple alt or alt_inline message types
* To generate iCal events, use the bundled extras/EasyPeasyICS.php class or iCalcreator
* @link http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/
* @link http://kigkonsult.se/iCalcreator/
* @var string
*/
public $Ical = '';
/**
* Stores the complete compiled MIME message body.
* @var string
@ -1577,6 +1589,11 @@ class PHPMailer {
$body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
$body .= $this->EncodeString($this->Body, $this->Encoding);
$body .= $this->LE.$this->LE;
if(!empty($this->Ical)) {
$body .= $this->GetBoundary($this->boundary[1], '', 'text/calendar; method=REQUEST', '');
$body .= $this->EncodeString($this->Ical, $this->Encoding);
$body .= $this->LE.$this->LE;
}
$body .= $this->EndBoundary($this->boundary[1]);
break;
case 'alt_inline':

90
extras/EasyPeasyICS.php Normal file
View File

@ -0,0 +1,90 @@
<?php
/* ------------------------------------------------------------------------ */
/* EasyPeasyICS
/* ------------------------------------------------------------------------ */
/* Manuel Reinhard, manu@sprain.ch
/* Twitter: @sprain
/* Web: www.sprain.ch
/*
/* Built with inspiration by
/" http://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calend/1464355#1464355
/* ------------------------------------------------------------------------ */
/* History:
/* 2010/12/17 - Manuel Reinhard - when it all started
/* ------------------------------------------------------------------------ */
class EasyPeasyICS {
protected $calendarName;
protected $events = array();
/**
* Constructor
* @param string $calendarName
*/
public function __construct($calendarName=""){
$this->calendarName = $calendarName;
}//function
/**
* Add event to calendar
* @param string $calendarName
*/
public function addEvent($start, $end, $summary="", $description="", $url=""){
$this->events[] = array(
"start" => $start,
"end" => $end,
"summary" => $summary,
"description" => $description,
"url" => $url
);
}//function
public function render($output = true){
//start Variable
$ics = "";
//Add header
$ics .= "BEGIN:VCALENDAR
METHOD:PUBLISH
VERSION:2.0
X-WR-CALNAME:".$this->calendarName."
PRODID:-//hacksw/handcal//NONSGML v1.0//EN";
//Add events
foreach($this->events as $event){
$ics .= "
BEGIN:VEVENT
UID:". md5(uniqid(mt_rand(), true)) ."@EasyPeasyICS.php
DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
DTSTART:".gmdate('Ymd', $event["start"])."T".gmdate('His', $event["start"])."Z
DTEND:".gmdate('Ymd', $event["end"])."T".gmdate('His', $event["end"])."Z
SUMMARY:".str_replace("\n", "\\n", $event['summary'])."
DESCRIPTION:".str_replace("\n", "\\n", $event['description'])."
URL;VALUE=URI:".$event['url']."
END:VEVENT";
}//foreach
//Footer
$ics .= "
END:VCALENDAR";
if ($output) {
//Output
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename='.$this->calendarName.'.ics');
echo $ics;
} else {
return $ics;
}
}//function
}//class

View File

@ -874,7 +874,44 @@ EOT;
}
}
/**
/**
* iCal event test
*/
function test_Ical()
{
$this->Mail->Body = 'This is the <strong>HTML</strong> part of the email.';
$this->Mail->AltBody = 'This is the text part of the email.';
$this->Mail->Subject .= ': iCal';
$this->Mail->IsHTML(true);
$this->BuildBody();
require_once '../extras/EasyPeasyICS.php';
$ICS = new EasyPeasyICS("PHPMailer test calendar");
$ICS->addEvent(
strtotime('tomorrow 10:00 Europe/Paris'),
strtotime('tomorrow 11:00 Europe/Paris'),
'PHPMailer iCal test',
'A test of PHPMailer iCal support',
'https://github.com/PHPMailer/PHPMailer'
);
$this->Mail->Ical = $ICS->render(false);
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
$this->Mail->Body = 'Embedded Image: <img alt="phpmailer" src="cid:my-attach">' .
'Here is an image!</a>.';
$this->Mail->AltBody = 'This is the text part of the email.';
$this->Mail->Subject .= ': iCal + inline';
$this->Mail->IsHTML(true);
$this->Mail->AddEmbeddedImage(
'../examples/images/phpmailer.png',
'my-attach',
'phpmailer.png',
'base64',
'image/png'
);
$this->BuildBody();
$this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
}
/**
* Test sending multiple messages with separate connections
*/
function test_MultipleSend()
@ -1139,21 +1176,22 @@ EOT;
unlink($keyfile);
}
/**
* Test line break reformatting
*/
function test_LineBreaks()
{
$unixsrc = "Hello\nWorld\nAgain\n";
$macsrc = "Hello\rWorld\rAgain\r";
$windowssrc = "Hello\r\nWorld\r\nAgain\r\n";
$mixedsrc = "Hello\nWorld\rAgain\r\n";
$target = "Hello\r\nWorld\r\nAgain\r\n";
$this->assertEquals($target, PHPMailer::NormalizeBreaks($unixsrc), 'UNIX break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($macsrc), 'Mac break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($windowssrc), 'Windows break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($mixedsrc), 'Mixed break reformatting failed');
}
/**
* Test line break reformatting
*/
function test_LineBreaks()
{
$unixsrc = "Hello\nWorld\nAgain\n";
$macsrc = "Hello\rWorld\rAgain\r";
$windowssrc = "Hello\r\nWorld\r\nAgain\r\n";
$mixedsrc = "Hello\nWorld\rAgain\r\n";
$target = "Hello\r\nWorld\r\nAgain\r\n";
$this->assertEquals($target, PHPMailer::NormalizeBreaks($unixsrc), 'UNIX break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($macsrc), 'Mac break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($windowssrc), 'Windows break reformatting failed');
$this->assertEquals($target, PHPMailer::NormalizeBreaks($mixedsrc), 'Mixed break reformatting failed');
}
/**
* Miscellaneous calls to improve test coverage and some small tests
*/