dataDesk BLOG

Welcome to the blog of Mr.G

Nature

/Dev/HowTo

(Last modified: March 19 2025 17:43:31)

phpmailer


composer require phpmailer/phpmailer

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php'; // Load PHPMailer via Composer

function sendAuctionEmail($recipientEmail, $recipientName, $subject, $message) {
    $mail = new PHPMailer(true);

    try {
        // SMTP settings
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com'; // SMTP server (use your provider if not Gmail)

        //$mail->Host = 'myappz.com'; 
        //$mail->Hostname = 'mail.myappz.com';

        $mail->SMTPAuth = true;
        $mail->Username = 'your-email@gmail.com'; // Your email address
        $mail->Password = 'your-email-password'; // Use an "App Password" if using Gmail
        //$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; 
        //$mail->Port = 587;
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port = 465;

        // $mail->SMTPDebug = 2; // Use 2 for verbose output
        // $mail->Debugoutput = function ($str, $level) {
        //     file_put_contents('phpmailer_debug.log', "[$level] $str\n", FILE_APPEND);
        // };

        // Email headers
        $mail->setFrom('your-email@gmail.com', 'Auction Alerts'); // Sender email & name
        $mail->addAddress($recipientEmail, $recipientName); // Recipient email & name
        $mail->Subject = $subject;
        $mail->Body = $message;
        $mail->isHTML(true); // Allows HTML content (optional)

        // Send email
        $mail->send();
        return "📧 Email sent successfully to $recipientEmail";
    } catch (Exception $e) {
        return "❌ Email failed: " . $mail->ErrorInfo;
    }
}


require 'sendmail.php';

// Example: Send an email when 15 minutes remain
$recipientEmail = "user@example.com";
$recipientName = "John Doe";
$subject = "Auction Alert: 15 Minutes Left!";
$message = "Hello $recipientName, this is a reminder that your auction is ending in 15 minutes.";

echo sendAuctionEmail($recipientEmail, $recipientName, $subject, $message);

Gmail (and other email providers) require proper email authentication to prevent spam and to verify that emails are coming from legitimate sources. This typically involves SPF (Sender Policy Framework) and DKIM (DomainKeys Identified Mail).

Here’s how to set up SPF and DKIM to make your email deliverable to Gmail and other providers:


1️⃣ SPF (Sender Policy Framework)

SPF is used to verify the sending mail server is authorized to send emails on behalf of your domain.

Steps to Set Up SPF:

  1. Log in to your DNS provider (where you manage your domain settings).

  2. Add a TXT record for SPF:

    • Host: @ (or your domain name)
    • Type: TXT
    • Value:
      v=spf1 a mx ip4:12.123.123.12 ~all
    • Explanation:
      • v=spf1 → Specifies it's an SPF record.
      • a → Accepts mail from the IP address of the A record.
      • mx → Accepts mail from any server listed in the MX records for the domain.
      • ip4:12.123.123.12 → Allows mail from the IP 12.123.123.12 (your server IP).
      • ~all → Soft fail (emails from unauthorized servers are marked but not rejected).
  3. Save and apply the record. It may take a few minutes for DNS propagation.


2️⃣ DKIM (DomainKeys Identified Mail)

DKIM adds a digital signature to the email, allowing the receiving mail server (like Gmail) to verify that the email was sent by an authorized server and that it wasn’t tampered with during transit.

Steps to Set Up DKIM:

  1. Generate DKIM Keys using OpenSSL or a service like EasyDKIM or Plesk (if you're using it).

    • You'll get a private key for signing emails and a public key to publish in your DNS.
  2. Publish the DKIM public key in your DNS as a TXT record:

    • Host: default._domainkey
    • Type: TXT
    • Value:
      v=DKIM1; k=rsa; p=MIGfMA0GCSq...<your public key>...Q1cQ
  3. Configure Postfix (or your mail server) to sign outgoing emails with your private DKIM key.


3️⃣ DMARC (Optional but Recommended)

DMARC (Domain-based Message Authentication, Reporting & Conformance) is used to ensure your emails are being authenticated properly with SPF and DKIM. It's optional, but highly recommended to increase your domain's email security.

Steps to Set Up DMARC:

  1. Create a TXT record in your DNS for DMARC:

    • Host: _dmarc
    • Type: TXT
    • Value:
      v=DMARC1; p=none; rua=mailto:dmarc-reports@mydomain.com
  2. Explanation:

    • v=DMARC1 → Specifies it’s a DMARC record.
    • p=none → Policy (set to "none" to only monitor without rejecting).
    • rua=mailto:dmarc-reports@mydomain.com → Where to send reports about DMARC failures.

4️⃣ Test Your Setup

Once you have set up SPF, DKIM, and optionally DMARC, you can test your email authentication using tools like:


🚀 Final Checklist for Gmail:

  1. SPF: Add an SPF TXT record to DNS.
  2. DKIM: Set up DKIM signing in your mail server and publish the public key in DNS.
  3. DMARC: (Optional) Add a DMARC TXT record for reporting.

Once all of this is in place, emails from smtp.mydomain.com will pass Gmail’s SPF and DKIM checks, making them more likely to be delivered to the inbox.



Who I am

"Just me, myself, and I, exploring the universe of unknownment, embracing the mystery of what lies beyond, one star at a time."

Share this page on Facebook | X.com | Reddit

Latest Posts

  • Image Lem - Warren Buffett walked into a University of Florida classroom in 1998 ... [2098423133938356224] (1)
    Published: September 12 2026
    Read
  • Image Sam Stoffel - Jack Dorsey's simple rule for success in business [2089319019065057280]
    Published: August 19 2026
    Read
  • Image Peter-Steinberger-What-Happens-When-4.7-_1786407922546
    Published: August 16 2026
    Read
  • Image Monitor_server_action____
    Published: August 16 2026
    Read
  • Image Disk_and_RAID_testing_commands
    Published: July 31 2026
    Read

Tags

WebDev New York London AI NORWAY DIY Ideas Life Family News Clothing Invest Sports Health