How to apply normal mail function in wordpress
In WordPress, you can use the wp_mail()
function to send normal mail (also known as email) from your WordPress site. Here's a step-by-step guide on how to use it:
1. Define the recipient's email address and subject
You need to define the recipient's email address and the subject of the email. You can do this by using PHP variables or by retrieving the values from a form submission.
For example:
$to = '[email protected]';
$subject = 'Test Email from WordPress';
2. Define the email message
You need to define the email message that you want to send. You can use HTML or plain text format. For example:
$message = 'This is a test email sent from WordPress.';
3. Use the wp_mail()
function
The wp_mail()
function takes three arguments: the recipient's email address, the subject, and the email message. You can use it like this:
wp_mail($to, $subject, $message);
4. Optional: Add attachments
If you want to send attachments with your email, you can use the wp_mail()
function's fourth argument, which is an array of attachment files. For example:
$attachments = array('path/to/file1.pdf', 'path/to/file2.txt');
wp_mail($to, $subject, $message, $attachments);
5. Optional: Set the email headers
You can also set custom email headers using the wp_mail()
function's fifth argument, which is an array of headers. For example:
$headers = array('From: Your Name <[email protected]>', 'Content-Type: text/html');
wp_mail($to, $subject, $message, '', $headers);
Example code
Here's an example code that sends a simple email using the wp_mail()
function:
<?php
// Define the recipient's email address and subject
$to = '[email protected]';
$subject = 'Test Email from WordPress';
// Define the email message
$message = 'This is a test email sent from WordPress.';
// Use the wp_mail() function
wp_mail($to, $subject, $message);
// Optional: Add attachments
$attachments = array('path/to/file1.pdf', 'path/to/file2.txt');
wp_mail($to, $subject, $message, $attachments);
// Optional: Set the email headers
$headers = array('From: Your Name <[email protected]>', 'Content-Type: text/html');
wp_mail($to, $subject, $message, '', $headers);
?>
Tips and considerations
- Make sure to use a valid email address and subject for the recipient.
- Use a valid email message and format (HTML or plain text).
- If you're sending attachments, make sure the file paths are correct and the files exist.
- If you're setting custom email headers, make sure they are valid and follow the email header format.
- The
wp_mail()
function uses the WordPress site's email settings (e.g., SMTP settings) to send the email. If you're having issues with email delivery, check your site's email settings.