Skip to content

Instantly share code, notes, and snippets.

@ojulianos
Last active December 22, 2021 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ojulianos/c18162463d5d72fd285be7b1e3e68c18 to your computer and use it in GitHub Desktop.
Save ojulianos/c18162463d5d72fd285be7b1e3e68c18 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__ . '/../mail/v5_2/PHPMailerAutoload.php';
class InviteTest {
protected $code_invite;
protected $curr_date;
protected $event_date_ini;
protected $event_date_fin;
protected $organizer;
protected $location;
protected $attendees;
protected $rules;
protected $method;
/**
* @param string $organizer [email do organizado]
* @param string $method [REQUEST, REPLY, CANCEL]
*/
public function __construct($organizer, $method)
{
$curr_date = new DateTime();
$this->code_invite = md5('esaf_' . date('d/m/y-h-i-s'));
$this->curr_date = $this->formataData($curr_date);
$this->organizer = $organizer;
$this->method = $method;
$this->location = '';
$this->attendees = '';
$this->rules = '';
}
/**
* @param string $subject
* @param string $desctiption
*/
public function sendInvite($subject, $desctiption)
{
$invite = $this->geraInvite($subject, $desctiption);
return $this->mandaEmail($invite);
}
/**
* @param string $event_ini [data de início do evento]
* @param string $event_end [data de fim do evento]
* @return void
*/
public function setDates($event_ini, $event_end)
{
$ini = new DateTime($event_ini);
$end = new DateTime($event_end);
$this->event_date_ini = $this->formataData($ini);
$this->event_date_fin = $this->formataData($end);
}
/**
* @param string $name [nome do local ou veiculo reservado]
* @param string $email [email do local ou veiculo reservado]
*/
public function setLocation($name, $email)
{
$location = new stdClass;
$location->name = $name;
$location->email = $email;
$this->location = "LOCATION:{$location->name} <{$location->email}>";
return $this;
}
/**
* @param string $name [nome de quem vai receber a notificacao]
* @param string $email [email de quem vai receber a notificacao]
* @param string $status [ACCEPTED, DECLINED, NEEDS-ACTION]
*/
public function addAttendee($name, $email, $status)
{
$attendee = new stdClass;
$attendee->name = $name;
$attendee->email = $email;
$attendee->status = $status;
$this->attendees .= "ATTENDEE;RSVP=TRUE;PARTSTAT={$attendee->status};CN={$attendee->name}:{$attendee->email};MAILTO:{$attendee->email} \n";
return $this;
}
/**
* @param string $frequencia
* @param string $periodo
* @param string $dias
*/
public function addRule($frequencia, $periodo, $dias)
{
$rule = new stdClass;
$rule->frequencia = $frequencia;
$rule->periodo = $periodo;
$rule->dias = $dias;
$this->rules .= "RRULE:INTERVAL=1;FREQ={$rule->frequencia};UNTIL={$rule->periodo};BYDAY={$rule->dias} \n";
return $this;
}
/**
* @param string $organizer email
* @param array $attendees array[array[PARTSTAT, CN, EMAIL]]
* @param array $location array[name, email]]
*/
private function geraInvite(
$subject,
$description
){
$header = "BEGIN:VCALENDAR\n
VERSION:2.0\n
PRODID:Zimbra-Calendar-Provider\n
METHOD:{$this->method}\n
BEGIN:VEVENT\n
ORGANIZER;SENT-BY=MAILTO:{$this->organizer}:MAILTO:workflow@ibrap.com.br\n";
$footer = "BEGIN:VALARM\n
TRIGGER:-PT15M\n
ACTION:DISPLAY\n
DESCRIPTION:Lembrar\n
END:VALARM\n
END:VEVENT\n
END:VCALENDAR\n";
return "{$header}
UID:{$this->code_invite}\n
{$this->rules}
{$this->attendees}
DTSTAMPTZID=America/Sao_Paulo:{$this->curr_date}\n
DTSTART;TZID=America/Sao_Paulo:{$this->event_date_ini}\n
DTEND;TZID=America/Sao_Paulo:{$this->event_date_fin}\n
{$this->location}
SUBJECT:{$subject}\n
DESCRIPTION:{$description}\n
{$footer}";
}
private function mandaEmail($ical){
$mail = new PHPMailer();
$mail->SMTPDebug = 2;
$mail->isSMTP(); // use SMTP
$mail->isHTML(false); // Define que o e-mail será enviado como HTML
$mail->ContentType = 'text/html';
$mail->Host = "seuhost"; // Endereço do servidor SMTP
$mail->Port = 25;
$mail->From = 'seuemail'; // Seu e-mail
$mail->FromName = 'seunome'; // Seu nome
$mail->Subject = 'Sala de Reunião Exclusivi';
$mail->addAddress('seuemail', 'seunome');
$mail->addStringAttachment($ical, "meeting.ics");
$mail->Ical = $ical;
$mail->Body = $ical;
return $mail->send();
}
private function formataData($date)
{
return substr_replace('-', 'T', $date->format('Ymd-His'));
}
}
$evento = isset($argv[1]) ? $argv[1] : $_REQUEST['evento'];
$invite = new InviteTest;
if(method_exists($invite, $evento)) {
return $invite->$evento();
}
echo 'Evento não cadastrado' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment