hi_009_wordpress_plugin_mod

[9] How I Built a Dual-Language Email Page Sender Plugin for WordPress

When I first started this experiment, the prompt was simple:

“Is there a WordPress plugin that lets me send the current page’s text to any email entered in a form?”

From there, things evolved – quite a lot.

What began as a theoretical idea turned into a fully functional, bilingual plugin that detects the site language, integrates with SMTP, and securely delivers any page’s content to a recipient’s inbox.

This post documents how that happened.

Step 1: The Initial Idea

The original concept was minimalistic:

  • Add a single input field to a WordPress page.
  • When the visitor enters an email, send the full text of that page to them.

At first, I tried to use Contact Form 7 for this, but soon realized it would require too many hooks and workarounds.

A custom plugin would be cleaner – just one PHP file, one function, no external dependencies.

Step 2: Building the Core Plugin

I started by creating a folder inside wp-content/plugins/:

/wp-content/plugins/email-page-sender/
and added a file named email-page-sender.php.

 

The first working version looked like this:

<?php
/**
 * Plugin Name: Email Page Sender
 * Description: Sends the current page content to a user-provided email address.
 */

add_action('the_content', function($content) {
    if (is_page()) {
        $form = '
        <form method="post" class="email-page-sender">
            <input type="email" name="eps_email" placeholder="Enter your email" required>
            <button type="submit" name="eps_submit">Send</button>
        </form>';
        return $content . $form;
    }
    return $content;
});

add_action('template_redirect', function() {
    if (isset($_POST['eps_submit']) && isset($_POST['eps_email'])) {
        $email = sanitize_email($_POST['eps_email']);
        if (is_email($email)) {
            global $post;
            $subject = 'Page: ' . get_the_title($post->ID);
            $body = wpautop(apply_filters('the_content', $post->post_content));
            wp_mail($email, $subject, $body);
        }
    }
});

 

This minimal version worked – but only under ideal conditions.

If your server didn’t support PHP mail or lacked SMTP configuration, nothing was actually sent.

Step 3: Adding SMTP and Proper Headers

After testing, the first email failed due to Gmail’s DMARC policy:

“Unauthenticated email from gmail.com is not accepted…”

That’s when I integrated WP Mail SMTP and switched the “From” address to my domain (noreply@online-dentist.hu).

The fixed mail section became:

$headers = [
  'Content-Type: text/html; charset=UTF-8',
  'From: Online Dentist <noreply@online-dentist.hu>',
  'Reply-To: schulmann.istvan@gmail.com'
];

$sent = wp_mail($email, $subject, $body, $headers);

 

Now the email passed DMARC and SPF validation perfectly.

The plugin also displayed a small JavaScript alert confirming success or failure.

Step 4: Handling Page Context Correctly

Originally, the plugin hooked into init, but that ran before WordPress loaded the $post object –
resulting in empty emails.

Switching to template_redirect solved this neatly:

add_action('template_redirect', function() {
  // email logic here
});

 

At that point, $post was fully accessible, and the content sent correctly every time.

Step 5: Adding Dual-Language Support

My site has both Hungarian and English sections (URLs like /en/…),
so I wanted the form labels and alerts to match the current language.
To detect the language, I used a simple URL check:

function eps_get_language() {
    $url = $_SERVER['REQUEST_URI'] ?? '';
    return (strpos($url, '/en/') !== false) ? 'en' : 'hu';
}

 

Then I used this helper to localize text dynamically:

$lang = eps_get_language();

if ($lang === 'en') {
    $label = 'Would you like to receive this page by email?';
    $button = 'Send';
} else {
    $label = 'Szeretnéd megkapni ezt az oldalt emailben?';
    $button = 'Küldés';
}

 

and finally output the localized form:

<form method="post" class="email-page-sender">
    <p><strong><?php echo esc_html($label); ?></strong></p>
    <input type="email" name="eps_email" placeholder="Add meg az email címet" required>
    <button type="submit" name="eps_submit"><?php echo esc_html($button); ?></button>
</form>

 

Step 6: Showing Confirmation Alerts

To make it user-friendly, I added success/error messages injected into the footer:

add_action('wp_footer', function() use ($sent, $lang) {
    $msg = $sent
      ? ($lang === 'en' ? '✅ The page content has been sent!' : '✅ Az oldal tartalmát elküldtem!')
      : ($lang === 'en' ? '⚠️ Error sending email.' : '⚠️ Hiba történt az email küldése közben.');
    echo "<script>alert(" . json_encode($msg) . ");</script>";
});

 

It’s simple, but surprisingly effective.

What I Learned

  • Never rely on PHP’s mail() in production. SMTP authentication is a must, especially when using Gmail or custom domains.
  • DMARC and SPF records matter. Without proper DNS setup, even valid emails may bounce or be marked as spam.
  • Hook placement in WordPress is critical. init might be too early; template_redirect ensures post data is ready.
  • Internationalization doesn’t always need heavy plugins. For simple bilingual sites, even URL-based logic can be lightweight and effective.

Final Thoughts

This started as a quick “can I do this?” experiment – but became a small, elegant plugin that I’ll actually use on my personal introduction pages.

Sometimes the most satisfying builds aren’t the big, complex systems.

They’re the small, human-focused touches – like letting someone you just met receive your page with one click.

And that’s exactly what Email Page Sender does: a tiny but polished detail in the bigger story of how I built IT.

Buy me a coffee?

If you enjoyed this story, you can buy me a coffee. You don’t have to – but it means a lot and I always turn it into a new adventure.

Buy a coffee for Steve

Subscribe

You'll receive an email notification for every new post!

No spam emails! Read my privacy policy for more info.

Steve

Who am I? Who are you reading? Who are you supporting?

Steve

Add a Comment

Your email address will not be published. Required fields are marked *