如何在zf2中使用smtp发送电子邮件?

时间:2014-05-09 08:59:12

标签: php email zend-framework2 smtp

我做了一个zf2动作,其中我想向保存在数据库中的用户发送电子邮件,因此我使用gmail发送电子邮件但电子邮件未发送给用户。

我如何在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' => 'marif252@gmail.com',
                'password' => 'password');

                $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
                $mail = new Zend_Mail();
                $mail->setBodyHtml($bodytext);
                $mail->setFrom('marif252@gmail.com');
                $mail->addTo($formData['email'], $formData['username']);
                $mail->setSubject('Profile Activation');
                $mail->send($transport);

如何在zf2中使用gmail发送电子邮件?

1 个答案:

答案 0 :(得分:9)

尝试使用像这样的smtp:

$message = new \Zend\Mail\Message();
$message->setBody('This is the body');
$message->setFrom('myemail@mydomain.com');
$message->addTo('someone@somedomain.com');
$message->setSubject('Test subject');

$smtpOptions = new \Zend\Mail\Transport\SmtpOptions();  
$smtpOptions->setHost('smtp.gmail.com')
            ->setConnectionClass('login')
            ->setName('smtp.gmail.com')
            ->setConnectionConfig(array(
                'username' => 'YOUR GMAIL ADDRESS',
                'password' => 'YOUR PASSWORD',
                'ssl' => 'tls',
            ));

$transport = new \Zend\Mail\Transport\Smtp($smtpOptions);
$transport->send($message);