2

我有一个使用zend框架一次发送多封电子邮件的表单,用冒号分隔它们。

有没有办法阻止发送数据库中已经存在的电子邮件并发送那些不存在的电子邮件?

这里有一些:

html -----

<form method="post" action="<?php echo $this->url(array(), 'inviteByEmail'); ?>">
            <table><tr>
                <td>Email Addresses:* <span class="right small_font" style="margin-right:25px;">* Each email address must be separated by a comma ( , )</span></td>
                <td></td>
            </tr><tr>
                <td><input class="input_long" type="text" name="emails" /></td>
            </tr><tr>
                <td><br/>Personalize your message:</td>
            </tr><tr>
                <td><textarea cols="49" rows="7" name="personal_message"  id="share-text" style="margin-bottom:8px;" placeholder="Download Ad-Aware and help me get PRO version" ></textarea></td>
            </tr><tr>
                <td rowspan="3" valign="top">
                    <input type="submit" class="btn_submit_step3" id="btn_friends" value="share_friends" /><br/>
                </td>
            </tr></table>
        </form>

控制器动作-----

public function inviteByEmailAction()
{
    $emails = $this->getRequest()->getParam('emails');
    $personal_message = $this->getRequest()->getParam('personal_message');

    $emails = explode(",", $emails);

    foreach ($emails as $email)
    {
        $email = trim($email);
        $validator = new Zend_Validate_EmailAddress();
        if ($validator->isValid($email)) 
        {
            if ($this->_helper->model('EmailInvites')->noRowByFields(array('email' => $email, 'referred_by_user_id' => $this->_helper->user()->id)))
            {
                $email_invite = $this->_helper->model('EmailInvites')->createRow();  
                $email_invite->email = $email;
                $email_invite->personal_message = $personal_message;
                $email_invite->referred_by_user_id = $this->_helper->user()->id;
                $email_invite->save();

                $this->_helper->user()->sendInviteEmail($email, $personal_message);
            }
        }
    }
    $this->getHelper('redirector')->gotoRoute(array(), 'invite');

}

并且模型具有发送电子邮件的功能。

4

1 回答 1

1

您应该在尝试发送电子邮件之前对其进行过滤。调查array_diff

这是完成工作的psedo函数:

/**
 * 
 * @param array $userEnteredEmails array with all user entered emails
 * @return array
 */
protected function _getEmailsThatAreNotInDb(array $userEnteredEmails)
{
    $db = null; //  your database object
    /* @var $db Zend_Db_Adapter_Abstract */

    // get all emails from DB that are in user provided list
    $query = $db->select()
            ->from('emails_table', array('email_address'))
            ->where('email_address IN (?)', $userEnteredEmails);

    $emailsInDb = $db->fetchCol($query);

    // get emails from user provided list that are not in database loaded list
    $emailNonInDb = array_diff($userEnteredEmails, $emailsInDb);

    return $emailNonInDb;
}
于 2012-12-18T18:37:13.980 回答