Viewing 6 replies - 1 through 6 (of 6 total)
  • Try this basic form using wp_mail(’email address’, ‘subject’, ‘message’); function to send out the email

    <?php
    
    //require_once(ABSPATH.'wp-blog-header.php');
    $my_email = '[email protected]';
    $subject = 'Testing a form..';
    if( isset( $_POST['send'] ) ) {
    	$text = isset( $_POST['text'] ) ? sanitize_text_field($_POST['text']) : '';
    	if( $text !== '' ) {
    		wp_mail($my_email, $subject, $text);
    		echo '<p>Email sent!</p>';
    	}
    }
    ?>
    <form action="" method="post">
    	<textarea name="text" placeholder="Enter your message here" rows="4" cols="50"></textarea>
    	<br />
    	<input type="submit" value="Send" name="send" />
    </form>
    Thread Starter demitsin

    (@demitsin)

    I tried this code and when i choose send, the message “email sent” is appeared but i don’t receive any email.

    Something which works is this:
    <form action=”MAILTO:[email protected]” method=”post” name=”” enctype=”text/plain”>

    Thanks for your help!

    Did you add your email address in this line $my_email = '[email protected]'; ? Are you trying to run the email on localhost?

    You’re welcome.

    Thread Starter demitsin

    (@demitsin)

    Well, i had writen something wrong. It works!

    One last question:
    My form comsist of a lot of fields. So, should i write the code

    $text = isset( $_POST['text'] ) ? sanitize_text_field($_POST['text']) : '';
    	if( $text !== '' ) {
    		wp_mail($my_email, $subject, $text);
    		echo '<p>Email sent!</p>';
    	}

    for each field seperately?

    Just add more additional fields in the form (HTML) ie:

    <textarea name="text" placeholder="Enter your message here" rows="4" cols="50"></textarea>
    <input type="email" name="your_email" placeholder="your email" />
    <input type="url" name="your_website" placeholder="your website" />

    And to get their values use $_POST asosiated with their name attributes:

    $_POST["your_email"]
    $_POST["your_website"]

    And since you’re just emailing the output to yourself (not inserting to a database) you don’t need to worry about sanitizing or stripping html so as to avoid SQL injection, just, add the values to $text variable which will be sent at the end:

    $text = isset( $_POST['text'] ) ? sanitize_text_field($_POST['text']) : '';
    
    $text .= isset( $_POST['my_email'] ) ? "\n" . $_POST['my_email'] : '';
    $text .= isset( $_POST['my_email'] ) ? "\n" . $_POST['my_email'] : '';
    
    if( $text !== '' ) {
    	wp_mail($my_email, $subject, $text);
    	echo '<p>Email sent!</p>';
    }

    Have fun with your code ??

    Thread Starter demitsin

    (@demitsin)

    OK! Thanks again for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Submit button’ is closed to new replies.