What are potential issues with using the mail() function in PHP to send emails from a contact form?

One potential issue with using the mail() function in PHP to send emails from a contact form is that it may not be reliable and could end up in the recipient's spam folder. To solve this, you can use a third-party email service provider like SendGrid or Mailgun to improve deliverability rates and provide more advanced email features.

// Example code using SendGrid to send an email from a contact form
require 'vendor/autoload.php';
$email = new \SendGrid\Mail\Mail();
$email->setFrom("from@example.com", "Sender Name");
$email->setSubject("Subject of the email");
$email->addTo("recipient@example.com", "Recipient Name");
$email->addContent("text/plain", "Hello, this is the content of the email");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    echo "Email sent successfully!";
} catch (Exception $e) {
    echo 'Email could not be sent. Error: ' . $e->getMessage();
}