我有一个表格可以立即发送多封电子邮件,使用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');
}
模型具有发送电子邮件的功能。
答案 0 :(得分: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;
}