Skip to content
PHP Logo

How can PHP be used to explore the spam level of emails sent via phpmail?

To explore the spam level of emails sent via phpmail, you can use a spam detection API like SpamAssassin. This API can analyze the content of the email and provide a spam score indicating the likelihood of the email being spam. By integrating SpamAssassin with your PHP script, you can automatically check the spam level of emails before sending them out.

php
// Set up the email content and headers
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email';
$headers = 'From: sender@example.com';

// Call the SpamAssassin API to get the spam score
$spamCheck = shell_exec("spamc -c < email.txt");
$spamScore = explode(" ", $spamCheck)[1];

// Check the spam score and send the email if it's not considered spam
if ($spamScore < 5) {
    mail($to, $subject, $message, $headers);
    echo 'Email sent successfully!';
} else {
    echo 'Email detected as spam, not sent.';
}