Note that there are some explanatory texts on larger screens.

plurals
  1. POwp_mail() in a loop, only sending to last address
    primarykey
    data
    text
    <p>I'm using a plugin that allows you to send an e-mail to <code>x</code> amount of people. All you have to do is enter their "name" and "e-mail address". These entries are named <code>friend_name[0]</code> &amp; <code>friend_email[0]</code> respectively. There are <code>+</code> &amp; <code>-</code> buttons for adding and removing entry fields. The number in the <code>$key</code> goes up and down relative to the number of fields. I have confirmed all the <code>$keys</code> change on the entry form.</p> <p>When the form is submitted and is processed through the loop, the e-mails all send, but only to the last e-mail entered. So if I enter 5 names and 5 e-mails, they send with this data:</p> <p><strong>Name:</strong> John Doe 1 <strong>E-Mail:</strong> JohnDoe5@gmail.com</p> <p><strong>Name:</strong> John Doe 2 <strong>E-Mail:</strong> JohnDoe5@gmail.com</p> <p><strong>Name:</strong> John Doe 3 <strong>E-Mail:</strong> JohnDoe5@gmail.com</p> <p><strong>Name:</strong> John Doe 4 <strong>E-Mail:</strong> JohnDoe5@gmail.com</p> <p><strong>Name:</strong> John Doe 5 <strong>E-Mail:</strong> JohnDoe5@gmail.com</p> <p>Here's the loop:</p> <pre><code>function invfr_sendmail() { $post = ( !empty( $_POST ) ) ? true : false; if( $post ) { $subject = invfr_get_settings( 'subject' ); $message = invfr_get_settings( 'message' ); $friends = $_POST['friend_email']; $headers = "From:" . $_POST['user_name'] . " &lt;" . $_POST['user_email']. "&gt;" . "\r\n"; $errors = array(); foreach ( $friends as $key =&gt; $friend ) { $name = stripslashes( $_POST['friend_name'][$key] ); $email = trim( $_POST['friend_email'][$key] ); // Check name if( !$name ) $errors[] = '#friend_name-' . $key; if( !$email ) $errors[] = '#friend_email-' . $key; if( $email &amp;&amp; !is_email( $email ) ) $errors[] = '#friend_email-' . $key; } // send email if( !$errors ) { foreach ( $friends as $key =&gt; $friend ) $mail = wp_mail( $email, invfr_tokens_replacement( $subject, $_POST, $key ), invfr_tokens_replacement( $message, $_POST, $key ), invfr_tokens_replacement( $headers, $_POST, $key ) ); if( $mail ) echo 'sent'; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload