My client wants to be able to send an e-mail from the Page Editing Screen to a number of e-mail addresses. Every page has a custom field called email_addresses that takes comma seperated addresses:

enter image description here

Every page will have a different set of addresses. I need a textarea below this field in which my client can enter the body of the email and a submit button below that to actually send the email.

I've added a form with a textarea and a submit form to the Page Editing Screen page using the add_meta_box() function:

    // Add metabox
    function metabox_after_publish() {
       add_meta_box( 'send-email', 'Send E-mail', 'send_email_metabox_content', 'page', 'advanced', 'high' );
    add_action( 'add_meta_boxes', 'metabox_after_publish' );

    // callback function to populate metabox
    function send_email_metabox_content() { ?>
             <textarea id="email_content" style="width:100%" rows="6"></textarea>
             <p><input id="email_send_button" type="submit" class="button button-primary button-large" value="Send" /></p>
    <?php }

enter image description here

How can I make the form function so it sends the content of the textarea to the email addresses in the custom field?

  • you need a plugin to do this. do you know how to do a plugin ? – mmm Mar 29 '16 at 10:08
  • I do but I could use some pointing in the right direction. – kvd Mar 29 '16 at 10:29
  • Please provide us with some information on what you have tried yourself so far, where you're at, what's working and what isn't. We're not going to do your work for you :-). – engelen Mar 29 '16 at 11:54
  • I've updated the question. All the nessecary elements (custom field with email addresses, textarea and submit button) are now on the page. It just needs to be tied together. Unfortunately I lack the kowledge to do so. – kvd Mar 29 '16 at 12:52

First, decide whether you want to use GET or POST for your form. (without specifying the method, it will use GET.)

You will need the name attribute filled out for your textarea, so the info can actually be sent. Then add your form processing into your function:

(i.e. if ( isset($_POST["message"]) && !empty($_POST["message"]) ) {...} )

You will need to use get_post_meta( get_the_ID(), 'email_addresses' ) to retrieve the email addresses from the custom field. Store that value in a string variable. You will want to separate out the email addresses into an array (beware of the space - you should make sure your client knows if they should or shouldn't have a space after the commas).

Then, you'll need to loop through the array with a foreach, (because the PHP mail() function's $to parameter does not accept arrays, only strings). You can build out your email form processing like the examples on this page.

Hope this helps! Let me know if you need any clarification.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.