Comment and email


#1

Hello everyone

I’m trying to combine a commenting system from Starckio and email sending. I would like to send an email when a visitor add a comment. I build eveything step by step, the comment plugin is working but I don’t receive the email. I tried to copy-paste the logic from Bastian simple contact form. So this is my code :

The email driver :

email::$services['html-mail'] = function($email) {
  $headers = header::create([
    'From'                      => $email->from,
    'Reply-To'                  => $email->replyTo,
    'Cc'                        => $email->cc,
    'Bcc'                       => $email->bcc,
    'Return-Path'               => $email->replyTo,
    'Message-ID'                => '<' . time() . '-' . $email->from . '>',
    'X-Mailer'                  => 'PHP v' . phpversion(),
    'Content-Type'              => 'text/html; charset=utf-8',
    'Content-Transfer-Encoding' => '8bit',
  ]);
  ini_set('sendmail_from', $email->from);
  $send = mail(
    $email->to,
    str::utf8($email->subject),
    str::utf8($email->body),
   $headers
  );
  ini_restore('sendmail_from');
  if(!$send) {
    throw new Error('The email could not be sent');
  }
};  

The controller :

$alert = null;
  if(get('submit')) {

    $data = array(
      'name'  =>  filter_var(get('name'), FILTER_SANITIZE_STRING),
      'email' => filter_var(get('email'), FILTER_SANITIZE_STRING),
      'comment'  => filter_var(get('comment'), FILTER_SANITIZE_STRING),
      'date' => date('Y-m-d'),
      'time' => date('H:i')
    );
    $rules = array(
      'name'  => array('required'),
      'email' => array('required', 'email'),
      'comment'  => array('required', 'min' => 1, 'max' => 1024),
    );
    $messages = array(
      'name'  => 'Please enter a valid name',
      'email' => 'Please enter a valid email address',
      'comment'  => 'Please enter a comment between 1 and 1024 characters',
    );

    // some of the data is invalid
    if($invalid = invalid($data, $rules, $messages)) {
      $alert = $invalid;
      // the data is fine, let's save the comment
    } else {

      
      // build the email
      $email = email(array(
        'to'      => 'myemail@yahoo.fr',
        'from'    => 'myemail@yahoo.fr',
        'subject' => 'New comment',
        'body'    => 'Hey, this is a test email!',
        'service' => 'html-mail'
      ));
      
      try {
        $comments = yaml($page->comments());
        $comments[] = $data;

        page()->update(array(
          'comments' => yaml::encode($comments),
        ));

        go('#comments');

      } catch(Exception $e) {
        echo $e->getMessage();
      }

    }
  }

(The myemail@yahoo.fr is for this thread) I don’t know why I don’t receive the email because I think I have put everyting at the right place…and I don’t really want to use Uniform because I don’t have any forms on other pages, and I don’t think it could change something.


#2

Here is a functional code:

<?php
return function($site, $pages, $page) {
  $alert = null;
  if(get('submit')) {
    $data = array(
      'name'  =>  filter_var(get('name'), FILTER_SANITIZE_STRING),
      'email' => filter_var(get('email'), FILTER_SANITIZE_STRING),
      'comment'  => filter_var(get('comment'), FILTER_SANITIZE_STRING),
      'date' => date('Y-m-d'),
      'time' => date('H:i')
    );
    $rules = array(
      'name'  => array('required'),
      'email' => array('required', 'email'),
      'comment'  => array('required', 'min' => 1, 'max' => 1024),
    );
    $messages = array(
      'name'  => 'Please enter a valid name',
      'email' => 'Please enter a valid email address',
      'comment'  => 'Please enter a comment between 1 and 1024 characters',
    );
    // some of the data is invalid
    if($invalid = invalid($data, $rules, $messages)) {
      $alert = $invalid;
    // the data is fine, let's save the comment
    } else {
      try {
        $comments = yaml($page->comments());
        $comments[] = $data;
        page()->update(array(
          'comments' => yaml::encode($comments),
        ));
        
        // Build the email text
        $to      = 'ton email';
        $from    = 'noreply@'.str_replace('www.', '', server::get('server_name'));
        $subject = 'Nouveau commentaire sur le site…blablabla';
        $body    = $data['comment'];
        // Send the confirmation email
        $email = new Email(array(
        	'to'      => $to,
        	'from'    => $from,
        	'subject' => $subject,
        	'body'    => $body
        ));
        if(v::email($from) and v::email($to) and $email->send()) {
        	$success = 'Message envoyé avec success';
        } else {
        	$error = 'message non envoyé';
        }
        
        go($site->page()->url());
      } catch(Exception $e) {
          echo $e->getMessage();
        }
    }
  }
  return compact('alert', 'success', 'error');
};

Do not forget the alerts:

<?php if(isset($success)): ?>
<div class="alert success">
	<p><?= $success ?></p>
</div>
<?php endif ?>

<?php if($error): ?>
<div class="alert error">
	<p><?= $error ?></p>
</div>
<?php endif ?>

#3

Thank you very much starckio, I will send you very soon the project I’m building with two plugins you created, I love them !!!

Bises


#4

Thank. :slightly_smiling_face:
I can not wait to see your work!