funtkungus
funtkungus

Reputation: 246

Is it possible to create html elements for email templates via a loop?

I have a working phpMailer that sends out various email templates; emails for "forgot password" or "forgot username", etc.

I was curious if it was possible to create html elements via a while loop? (I'm sorry I can't explain it so well, so here is my current code. It works but it just doesn't do the loop that I want it to do. It sends an email with just one username).

for ($i=0; $i < count($w_user_id_infos); $i++) { 
    $replaceUsername = $w_user_id_infos[$i]["login_cd"];;

    for ($j=0; $j < count($w_user_id_infos[$i]["customer_info"]); $j++) { 
        $w_customer_name1_text = $w_user_id_infos[$i]["customer_info"][$j]["customer_name1_text"];
        $w_customer_name2_text = $w_user_id_infos[$i]["customer_info"][$j]["customer_name2_text"];
        $w_body .= ($j + 1) . " " . $w_customer_name1_text . " / " . $w_customer_name2_text . "<br>";
        
        $replacementValues = [
            $w_customer_name1_text,
            $w_customer_name2_text
        ];
        $templateValues = [
            "%username%",
            "%company%",
            "%company2%"
        ];
    }
        
    array_splice($replacementValues, 0, 0, $replaceUsername);
    
    $w_body = file_get_contents('../../../framework/src/function/html_templates/forgot_username.html');
    $w_body = str_replace($templateValues, $replacementValues, $w_body);
}
$data = explode(',', $_SESSION['icon_data']);
$image_data = base64_decode($data[1]);

f_common_send_mail($w_addr, $w_subject, $w_body, $image_data);

So, for our program, a user can create multiple accounts with a single email address. The goal with the loop problem is, if that user forgot one (or more) of their usernames, they can click "forgot username" and after they enter their email address, an email would be sent with all of their usernames.

This is the html template.

<table bgcolor="#ffffff" class="table_content" align="center" cellpadding="0" cellspacing="0" border="0">
        <!-- HEAD -->
        <tr>
          <td bgcolor="#ffffff" class="header_detail_area">
              <table width="10%" align="left" border="0" cellpadding="0" cellspacing="0">  
                <tr>
                  <td style="padding: 0 20px 20px 0;">
                    <img src="cid:img" style="width: 65px; height: 65px; border-radius: 50%;">
                  </td>
                </tr>
              </table>
              <table width="70%" align="left" border="0" cellpadding="0" cellspacing="0">  
                <tr>
                    <td class="header_info">
                      STRUCT - ユーザー名の確認のお知らせ
                    </td>
                </tr>
              </table>
          </td>
        </tr>
         <!-- HEAD END -->
         <!-- USER INFO  -->
        <tr>
          <td class="user_detail_area borderbottom">
            <table width="97%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td class="user_info">
                   ユーザー名の確認を受け付けました。本メールアドレスには以下のユーザー名が紐づいています。                
                </td>
              </tr>
              <tr>
                <td class="user_info_blockquote">
                  <p class="blockquote">
                       <font style="color: #03abf2"> ユーザー名: </font> %username% <br>
                       <font style="color: #03abf2"> 所属会社: </font> %company% / %company2%
                  </p>          
                </td>
              </tr>
              <tr><td><br></td></tr>
            </table>
          </td>
        </tr>
        <!-- USER INFO END -->
        <!-- FOOTER DETAILS -->
        <tr>
          <td class="footer_detail_area" bgcolor="#31424b">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td align="center">
                 <img src="cid:Logo" width="200">
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <!-- FOOTER DETAILS END -->

    </table>

    </td>
  </tr>
</table>

I want to know if this is possible? To somehow create more of the %username% based on the for loop? The user currently receives the email with just the one username that is registered with their email address.

Is it possible to manipulate the html email template with a loop? My thought is that since it's using an already-made email template, it won't have the chance to add any more html elements? even if I added some string literals, I don't think I can loop inside a string literal.

Thank you in advance.

Upvotes: 1

Views: 746

Answers (1)

funtkungus
funtkungus

Reputation: 246

My solution:

I replaced the whole tr that needed to be looped:

<tr>
 <td class="user_info_blockquote">
  <p class="blockquote">
   <font style="color: #03abf2"> ユーザー名: </font> %username% <br>
   <font style="color: #03abf2"> 所属会社: </font> %company% / %company2%
  </p>          
 </td>
</tr>

changed to just:

%userElement%

then I created that html element in my loop like so:

$userElement .= 
                '<tr>
                    <td class="user_info_blockquote">
                        <p class="blockquote">
                            <font style="color: #03abf2"> ユーザー名: </font> '. $replaceUsername .' <br>
                            <font style="color: #03abf2"> 所属会社: </font> '. $w_customer_name1_text .' / '. $w_customer_name2_text .'
                        </p>          
                    </td>   
                </tr>';

Worked like a charm.

Upvotes: 1

Related Questions