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.
