Skip to content

Instantly share code, notes, and snippets.

@ojulianos
Created December 23, 2021 18:27
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/2bb7bbf54c16bbd78aa35aa379551c5b to your computer and use it in GitHub Desktop.
Save ojulianos/2bb7bbf54c16bbd78aa35aa379551c5b to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__ . '/../mail/v5_2/PHPMailerAutoload.php';
/**
* Classe para criar e enviar por email agendamentos
*
* MODO DE USO
* Instacie a classe
* $invite = new InviteICS('email do organizador', 'nome do organizador', 'tipo de evento', 'codigo');
*
* Informe a data de inicio e data de final do evento
* $invite->setDates('2021-12-23 19:00:00', '2021-12-23 20:00:00');
*
* Informe o local do evento
* $invite->setLocation('nome do local', 'email do local');
*
* Informe todos os participantes
* $invite->addAttendee('nome','email', 'status');
* $invite->addAttendee('nome','email', 'status');
*
* Adicione as regras caso necessário
* $invite->addRule('tipo', "{$data_fin}T{$hora_fin}", 'dias_semana');
*
* Execute o método para cadastrar e enviar o agendamento
* $enviar = $invite->sendInvite('assunto', 'mensagem');
*/
class InviteICS
{
protected $code_invite;
protected $curr_date;
protected $event_date_ini;
protected $event_date_fin;
protected $organizer;
protected $location;
protected $attendees;
protected $rules;
protected $method;
protected $subject;
protected $mail;
/**
* @param string $organizer_email
* @param string $organizer_name
* @param string $method [REQUEST, REPLY, CANCEL]
* @param string $uid
*/
public function __construct($organizer_email, $organizer_name, $method = 'REQUEST', $uid = null)
{
$curr_date = new DateTime();
$this->code_invite = $uid ? $uid : md5('ibrap_' . date('d/m/y-h-i-s'));
$this->curr_date = $this->formataData($curr_date);
$organizer = new stdClass;
$organizer->name = $organizer_name;
$organizer->email = $organizer_email;
$this->organizer = $organizer;
$this->method = $method;
$this->location = '';
$this->attendees = '';
$this->rules = '';
$this->emailInicializa();
}
/**
* @param string $subject
* @param string $desctiption
*/
public function sendInvite($subject, $desctiption)
{
$this->subject = utf8_encode($subject);
$invite = $this->geraInvite($desctiption);
// Para salvar o arquivo ao invés de enviar, remova o comentário da linha abaixo
// return file_put_contents($this->curr_date . $this->code_invite.".ics", $invite);
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}>" . PHP_EOL;
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}" . PHP_EOL;
$this->mail->addAddress($attendee->email, $attendee->name);
return $this;
}
/**
* @param string $frequencia [DAILY,WEEKLY,MONTHLY]
* @param string $periodo [YYYYMMDDTHHIISS]
* @param string $dias [SU,MO,TU,WE,TH,FR,SA]
*/
public function addRule($frequencia, $periodo, $dias)
{
$rule = new stdClass;
$rule->frequencia = $frequencia;
$rule->periodo = $periodo;
$rule->dias = $dias;
$byday = $frequencia != 'MONTHLY' ? 'BYDAY' : 'BYMONTHDAY';
$this->rules .= "RRULE:INTERVAL=1;FREQ={$rule->frequencia};UNTIL={$rule->periodo};{$byday}={$rule->dias} " . PHP_EOL;
return $this;
}
/**
* @param string $description
*/
private function geraInvite(
$description
) {
$header = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:Zimbra-Calendar-Provider\nMETHOD:{$this->method}\nBEGIN:VEVENT\nORGANIZER;SENT-BY=MAILTO:{$this->organizer->name}\n";
$footer = "BEGIN:VALARM\nTRIGGER:-PT15M\nACTION:DISPLAY\nDESCRIPTION:Lembrar\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR\n";
return "{$header}UID:{$this->code_invite}\n{$this->rules}{$this->attendees}DTSTAMPTZID=America/Sao_Paulo:{$this->curr_date}\nDTSTART;TZID=America/Sao_Paulo:{$this->event_date_ini}\nDTEND;TZID=America/Sao_Paulo:{$this->event_date_fin}\n{$this->location}SUBJECT:{$this->subject}\nDESCRIPTION:{$description}\n{$footer}";
}
private function mandaEmail($ical)
{
$this->mail->Subject = $this->subject;
$this->mail->addStringAttachment($ical, "meeting.ics", "7bit", "text/calendar; charset=utf-8; method={$this->method}");
$this->mail->Ical = $ical;
$this->mail->Body = $ical;
return $this->mail->send();
}
private function emailInicializa()
{
$mail = new PHPMailer();
// $mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->isHTML(false);
$mail->ContentType = 'text/calendar';
$mail->Host = ""; // Endereço do servidor SMTP
$mail->SMTPSecure = "tls";
$mail->Port = 25; // Porta do SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = 'usuario@email.com'; // Usuário do servidor SMTP
$mail->Password = 'senha'; // Senha do servidor SMTP
$mail->From = $this->organizer->email;
$mail->FromName = $this->organizer->name;
$this->mail = $mail;
}
private function formataData($date)
{
$dateFormated = $date->format('Ymd-His');
return str_replace('-', 'T', $dateFormated);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment