Question

I make a zf2 action in which i want to send email to user that saved in database so i used gmail for sending emails but email not sent to user.

How i send email in zf2?

Here is my code:

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);

How to send email using gmail in zf2?

Was it helpful?

Solution

Try using smtp like this:

$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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top