Send emails with CI4's Email service — configure SMTP, set recipients, HTML body, and attachments.
Sending Email
// app/Config/Email.php
public string $fromEmail = "noreply@example.com";
public string $fromName = "My App";
public string $protocol = "smtp";
public string $SMTPHost = "smtp.gmail.com";
public int $SMTPPort = 587;
public string $SMTPUser = "you@gmail.com";
public string $SMTPPass = ""; // use env variable
public string $SMTPCrypto = "tls";
// Send email
$email = ConfigServices::email();
$email->setFrom("me@example.com", "MyApp");
$email->setTo("user@example.com");
$email->setCC("cc@example.com");
$email->setSubject("Welcome!");
$email->setMessage(view("emails/welcome", ["user" => $user]));
$email->setAltMessage("Plain text version");
if ($email->send()) {
echo "Sent!";
} else {
log_message("error", $email->printDebugger());
}