Here’s a step-by-step example of how to send emails without landing in the spam folder using PHP SMTP.
We'll use the PHPMailer library, which simplifies sending emails via SMTP and helps improve deliverability. Following these steps, you’ll learn how to properly configure SMTP to avoid emails landing in the spam folder.
First, you need to install the PHPMailer library. You can do this using Composer.
composer require phpmailer/phpmailer
If you don’t have Composer, you can download PHPMailer manually from GitHub and include it in your project.
Create a new file send_email.php where you will write the script to send the email using PHPMailer with SMTP.
isSMTP(); // Use SMTP $mail->Host = 'smtp.example.com'; // Set the SMTP server (use your SMTP provider) $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = '[email protected]'; // SMTP username $mail->Password = 'your_password'; // SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to (587 is common for TLS) //Recipients $mail->setFrom('[email protected]', 'Your Name'); $mail->addAddress('[email protected]', 'Recipient Name'); // Add recipient $mail->addReplyTo('[email protected]', 'Reply Address'); // Add a reply-to address // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Test Email Subject'; $mail->Body = 'This is a test email sent using PHPMailer and SMTP.'; $mail->AltBody = 'This is a plain-text version of the email for non-HTML email clients.'; // Send the email $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }
PHPMailer Initialization:
SMTP Server Configuration:
Setting the Sender and Recipient:
Setting the Email Content:
Sending the Email:
To avoid emails going to the spam folder, it’s crucial to follow these best practices:
Use a Reputable SMTP Provider:
Using a trusted SMTP provider like Gmail, SendGrid, or Mailgun improves deliverability because they are less likely to be flagged as spam.
Authenticate Your Domain:
Set up SPF (Sender Policy Framework), DKIM (DomainKeys Identified Mail), and DMARC (Domain-based Message Authentication, Reporting & Conformance) records for your domain to verify the legitimacy of your emails.
Avoid Spammy Content:
Ensure your email content is clean and not flagged as spam. Avoid excessive use of all-caps, spammy words (like "free," "winner," etc.), and too many links.
Use Plain-Text Alternative:
Always include a plain-text version of your email ($mail->AltBody). Some email clients flag HTML-only emails as suspicious.
Avoid Free Email Services as Senders:
Use a professional email address from your own domain instead of free services like Gmail, Yahoo, etc., to avoid being flagged as spam.
Limit the Number of Recipients per Email:
If sending bulk emails, use proper bulk email services instead of sending to many recipients in one message to avoid being flagged for spamming.
Upload the send_email.php file to your server and run it in the browser or through the command line:
php send_email.php
If the configuration is correct, you’ll see the message:
Message has been sent
If there’s an error, it will display:
Message could not be sent. Mailer Error: {Error Message}
By using PHPMailer and a proper SMTP setup, you can ensure your emails are sent reliably and with a lower chance of landing in the spam folder. Here's a quick summary:
This approach ensures better deliverability and reduces the chances of your emails being marked as spam.
Feel free to follow me:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3