How to send an email in WordPress using PHP?

php files, you can call the wp_mail() function to send an email: wp_mail(“name@example.com”, “Subject”, “Message”); WordPress will send an email with the SMTP credentials you entered into the wp-config. php file.

<?php
$to = 'mytestemail@gmail.com';
$subject = 'Test email';
$message = 'Test';
$headers = "From: The Sender name <myemailaddress@gmail.com>";
wp_mail( $to, $subject, $message, $headers );
?>

You can use wp_mail. The good thing about wp_mail is that it uses whatever the default mail delivery service is configured in WordPress.

wp_mail( $to, $subject, $message, $headers );

If you can Use in PHP to use this code

mail($to, $subject, $message, $headers);

How to send the checkbox value to email?

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve and sanitize form data
    $fname = sanitize_text_field($_POST['fname']);
    $email = sanitize_email($_POST['email']);
    $interested = isset($_POST['interested']) ? array_map('sanitize_text_field', $_POST['interested']) : array();

    // Prepare the email
    $to = 'your-email@example.com'; // Replace with your email address
    $subject = 'New Form Submission';
    $message = "Name: $fname\n";
    $message .= "Email: $email\n";
    $message .= "Interested in:\n";
    foreach ($interested as $interest) {
        $message .= "- $interest\n";
    }
    $headers = array('Content-Type: text/plain; charset=UTF-8');

    // Send the email
    if (wp_mail($to, $subject, $message, $headers)) {
        echo '<p>Thank you for your submission!</p>';
    } else {
        echo '<p>There was an error sending your message. Please try again later.</p>';
    }
}
?>

<form action="" method="post">
  <input type="text" name="fname" class="msaw-form-input" required placeholder="Your Name" />
  <input type="text" name="email" class="msaw-form-input" required placeholder="Your Email" />
  <input type="checkbox" name="interested[]" value="Oriole Holiday"/><span>Oriole Holiday</span>
  <input type="checkbox" name="interested[]" value="Xorbeo" /><span>Xorbeo</span>
  <input type="submit" value="Submit">
</form>

How to send text in table format in mail body?

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve and sanitize form data
    $fname = sanitize_text_field($_POST['fname']);
    $email = sanitize_email($_POST['email']);
    $interested = isset($_POST['interested']) ? array_map('sanitize_text_field', $_POST['interested']) : array();

    // Prepare the email
    $to = 'your-email@example.com'; // Replace with your email address
    $subject = 'New Form Submission';

    // Create HTML message
    $message = '
    <html>
    <head>
        <style>
            table {
                width: 100%;
                border-collapse: collapse;
            }
            table, th, td {
                border: 1px solid black;
            }
            th, td {
                padding: 8px;
                text-align: left;
            }
            th {
                background-color: #f2f2f2;
            }
        </style>
    </head>
    <body>
        <h2>Form Submission Details</h2>
        <table>
            <tr>
                <th>Name</th>
                <td>' . esc_html($fname) . '</td>
            </tr>
            <tr>
                <th>Email</th>
                <td>' . esc_html($email) . '</td>
            </tr>
            <tr>
                <th>Interested in</th>
                <td>';
    
    if (!empty($interested)) {
        $message .= '<ul>';
        foreach ($interested as $interest) {
            $message .= '<li>' . esc_html($interest) . '</li>';
        }
        $message .= '</ul>';
    } else {
        $message .= 'None';
    }

    $message .= '
                </td>
            </tr>
        </table>
    </body>
    </html>';

    // Set content type header for HTML email
    $headers = array('Content-Type: text/html; charset=UTF-8');

    // Send the email
    if (wp_mail($to, $subject, $message, $headers)) {
        echo '<p>Thank you for your submission!</p>';
    } else {
        echo '<p>There was an error sending your message. Please try again later.</p>';
    }
}
?>

<form action="" method="post">
  <input type="text" name="fname" class="msaw-form-input" required placeholder="Your Name" />
  <input type="text" name="email" class="msaw-form-input" required placeholder="Your Email" />
  <input type="checkbox" name="interested[]" value="Oriole Holiday"/><span>Oriole Holiday</span>
  <input type="checkbox" name="interested[]" value="Xorbeo" /><span>Xorbeo</span>
  <input type="submit" value="Submit">
</form>

administrator

Leave a Reply

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