WordPress offers its own mailing system. I suggest you use that instead of PHP’s native mail.
wp_mail
accepts five arguments:
wp_mail( $to, $subject, $message, $headers, $attachments );
So your code can be written this way:
// Set the headers
$headers[] = 'From: ' . get_option('admin_email');
// Add the addresses to an array
foreach($emails as $mail) {
if(is_email($mail)) {
$to[] = $mail;
}
}
// Add the attachment
if($name) {
$att[] = PLUGIN_DIR.'uploads/'.date('Ymd').'_'.$name;
}
// Send the emails
$results = wp_mail( $to, $subject, $body, $headers, $att );
// Output the results
echo $results;
Simple and short.
Related Posts:
- WordPress refuses to send mail, “…your host may have disabled the mail() function”
- WordPress “phpmailer_init” not working for me
- How do I override the Message-ID header of wp_mail function?
- Mail not sent when I set HTML headers
- Processing a subscription form with POST method?
- Different Webhost and using Google Mail server: Is there a better way than SMTP?
- Redirect to another page after submission using wp_mail
- PHP 8, AJAX mail form to function.php doesn’t work
- How to retrieve the sender email with wp_mail()?
- Pass return-path additional parameter in wp_mail
- Wp_mail doesnt work
- get_post_meta is always empty when I use wp_mail
- How can I send an email using PHP?
- PhpMailer SMTP NOTICE: EOF caught while checking if connected
- PhpMailer SMTP NOTICE: EOF caught while checking if connected
- How to set SMTP programmatically
- WordPress new user / forgot password emails are broken
- wp_mail function timing out
- How to disable wordpress confirmation email for new users
- Why is my get_post_meta always empty
- Best way to create a user programatically
- Error when requesting password reset email – wp authentication
- How to setup SMTP for only one specific wp_mail()
- Send PDF link in email based on radio button selection
- User invite email not delivered – related to subdomain?
- Woocommerce HTML email option unavailable
- Trigger Woocommerce New User Email
- wp_mail not sending
- Why wp_mail() function isn’t sending any emails and displaying ‘0’ in Chrome ‘Network’ response
- Validate emails in array using foreach
- Attaching a pdf to Contact Form 7 e-mail via functions.php [closed]
- Setting up 2 SMTP accounts: 1 for wordpress and 1 for woocommerce
- Set a condition based on WooCommerce checkout city field while placing order
- WooCommerce – Email admin with new user details
- WP mail sent to MS Exchange recipients = occasional email corruption?
- How do you ensure that a user is recognised and available for use only when it is verified through a row in the database called ‘accountVerified’?
- Capture User Email Address When Filling Out Form
- Send multiple attachments with wp_mail in PHP
- Emailing passwords: Setup installed on home server using XAMPP
- wp_mail file attachment not being placed in upload folder?
- Website needs to be opened for wp_mail to work
- Auto 301 to full post permalink? (using : /archive/%post_id% to %post_name% )
- Can I get an email notification when media is uploaded to the media library?
- Notify users only on post publish
- Sending a custom form data in email through WP Mail Function
- Woocommerce Email attachments not working – file not being attached
- Pass Variables or Variable Place-Holder from Editor to PHP
- Get user custom field value on function.php
- Send notification email to admin for every new post published
- difference between sanitize_email ,FILTER_VALIDATE_EMAIL and input email type in html5
- Different SMTP based on domain
- Use of antispambot with $curauth->email
- How do I attach an invoice PDF to an email in the Dukapress plugin for WordPress?
- Contact form – problem with displaying message about sent mail
- Get page permalink in contact form
- How to setup the Email piping in WordPress plugin?
- Is it secure to use SMTP password in .php file in WordPress website?
- Send an e-mail on address with the link with disliked post
- comment_post (if comment is approved OR $comment_approved === 1) not working?
- Running SMTP Conditionally
- I want to fusion the product columns in email table order
- Woocommerce checkout page – custom field checkbox value into email
- Including user data in “new user notification email”
- Cron not sending wp-mail()
- Email always ends up in spam
- Premature end of script headers: wp-mail.php?
- Changing default WPMU emails to be send thorugh Campaign Monitor Transactional emails
- Send email to users when they get new followers
- Multisite – SMTP not working on other network sites
- Adding action item to admin users table and sending email
- How can disable wordpress emails notifications?
- Woocommerce custom field on emails outputs ‘Array’
- Using Custom Javascript and pHp to send email to myself when a user clicks on an input button but only works on Chrome, IE, and Micorosft Edge
- Trying send mail from Theme page
- Cron job to call php to email last 24 hours posts
- How to check “From Email” via WordPress before an email is sent
- Adding multiple emails to a
- How to show value from ACF plugin field in e-mail?
- How can I prevent wordpress from sending emails
- wp_mail vs mail functions and header arrays
- Dynamic Email Handler with ‘header (“Location: …’
- Customize the summary table of an order in the email
- Why does WordPress not send the user the email to add a password?
- Word press – JSPDF – Sending PDF as attachment from wp_mail – Special characters
- Why is my custom email notification after purchase not sending?
- Pass a PHP variable to another file
- Using Global Variables Expensive for PHP
- How to resolve error “Cookies are blocked due to unexpected output.”?
- Why when I instantiate wp_error in a validation method my user registration method stops working?
- Highlight Current Tag in wp_tag_cloud
- Use ajax from function.php
- Single.php back button to parent category
- Trigger popup in a php if/else statement
- Ajax loaded form replaces form action with Ajax url
- Custom Walker_Nav_Menu issue with variables on PHP 7.3
- Display parent and child taxonomies in separate drop down select fields
- How do I access post_meta key with php?
- Is there a way to search for a missing endif?
- Changing WordPress author name in database
- Insert wordpress tags below posts via functions.php