我做了一個zf2行動,我想發送電子郵件給保存在數據庫中的用戶,所以我使用gmail發送電子郵件,但電子郵件沒有發送給用戶。如何使用zf2中的smtp發送電子郵件?
我如何用zf2發送郵件?
這裏是我的代碼:
public function addAction()
{
//check permissions
if(!$this->acl->isAllowed($this->user->user_type, 'user.create'))
$this->redirect()->toRoute('admin_index');
//
$this->layout()->top_heading = 'Add User';
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
////////////////////////////////
$form = new UserForm();
$update=false;
$message='';
if($this->getRequest()->isPost())
{
$post = $this->getRequest()->getPost();
$form->setInputFilter($form->getInputFilter());
$form->setData($post);
if($form->isValid())
{
$formData=$form->getData();
$s = new User();
$user = $dm->getRepository('Calendar\Document\User')->findOneBy(array(
"username" => $formData['username']
));
$email = $dm->getRepository('Calendar\Document\User')->findOneBy(array(
"email" => $formData['email']
));
if($user || $email)
{
$update=2;
$message='User Already Exists.';
}
if($post['role']=='admin')
{
$update=2;
$message="Select Some Other Role.";
}
else
{
$s->setProperty('username',$post['username']);
$s->setProperty('password',md5($post['password']));
$s->setProperty('email',$post['email']);
$s->setProperty('user_type',$post['role']);
$s->setProperty('dataentered',date('Y-m-d H:m:i'));
$dm->persist($s);
$dm->flush();
//echo new Response($s->getProperty('id'));
//
$update=1;
$message='User Added Successfully.';
$form = new UserForm();
$config = array('ssl' => 'tls',
'auth' => 'login',
'username' => '[email protected]',
'password' => 'password');
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
$mail->setBodyHtml($bodytext);
$mail->setFrom('[email protected]');
$mail->addTo($formData['email'], $formData['username']);
$mail->setSubject('Profile Activation');
$mail->send($transport);
如何ZF2使用Gmail發送電子郵件?
看到這個:http://framework.zend.com/manual/2.0/en/modules/zend.mail.transport.html –
我用這個,但沒有電子郵件發送 –
電子郵件是否不在您的收件箱中,或者您收到錯誤消息?你檢查過垃圾郵件文件夾嗎?你有沒有檢查你的php_error.log? –