How to send attachment in mail wordpress
In WordPress, you can send an email with an attachment using the wp_mail
function. Here's an example code snippet:
<?php
// Set the email recipient
$to = '[email protected]';
// Set the email subject
$subject = 'Email with attachment';
// Set the email body
$message = 'This is the email body';
// Set the attachment file path and name
$attachment = array(
'path' => WP_CONTENT_DIR. '/uploads/attachment.pdf',
'name' => 'attachment.pdf',
'type' => 'application/pdf',
'encoding' => 'base64'
);
// Set the email headers
$headers = array(
'Content-Type: text/plain; charset=UTF-8',
'Content-Disposition: attachment; filename="'. $attachment['name']. '"'
);
// Send the email
wp_mail($to, $subject, $message, $headers, $attachment);
?>
In this example, we're setting the email recipient, subject, body, and attachment file path and name. We're also setting the email headers to specify the attachment file name and type.
You can also use the wp_mail
function with an array of attachments:
<?php
// Set the email recipient
$to = '[email protected]';
// Set the email subject
$subject = 'Email with attachments';
// Set the email body
$message = 'This is the email body';
// Set the attachments array
$attachments = array(
array(
'path' => WP_CONTENT_DIR. '/uploads/attachment1.pdf',
'name' => 'attachment1.pdf',
'type' => 'application/pdf',
'encoding' => 'base64'
),
array(
'path' => WP_CONTENT_DIR. '/uploads/attachment2.docx',
'name' => 'attachment2.docx',
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'encoding' => 'base64'
)
);
// Send the email
wp_mail($to, $subject, $message, array(), $attachments);
?>
In this example, we're setting an array of attachments, each with its own file path, name, type, and encoding.
You can also use the wp_mail
function with a custom email class:
<?php
class MyEmail {
public function send() {
// Set the email recipient
$to = '[email protected]';
// Set the email subject
$subject = 'Email with attachment';
// Set the email body
$message = 'This is the email body';
// Set the attachment file path and name
$attachment = array(
'path' => WP_CONTENT_DIR. '/uploads/attachment.pdf',
'name' => 'attachment.pdf',
'type' => 'application/pdf',
'encoding' => 'base64'
);
// Set the email headers
$headers = array(
'Content-Type: text/plain; charset=UTF-8',
'Content-Disposition: attachment; filename="'. $attachment['name']. '"'
);
// Send the email
wp_mail($to, $subject, $message, $headers, $attachment);
}
}
// Create an instance of the email class
$email = new MyEmail();
// Send the email
$email->send();
?>
In this example, we're creating a custom email class that extends the WP_Email
class. We're overriding the send
method to set the email recipient, subject, body, and attachment, and then sending the email using the wp_mail
function.
Note that the wp_mail
function uses the wp_mail_attachment
filter to allow plugins and themes to modify the attachment handling. You can use this filter to add custom attachment handling or to modify the default attachment handling.