【问题标题】:Send link with variable using Zend_Mail使用 Zend_Mail 发送带有变量的链接
【发布时间】:2023-04-03 03:29:02
【问题描述】:

我正在尝试在用户完成注册后发送带有链接的电子邮件。 该链接应该有一个带有用户 ID 的变量 $id。 我尝试了不同的方法,但我的链接总是显示为
http://localhost/users-data/activate/.php?id=>

我正在使用 Zend_Mail。 我正在尝试做的是例如:向用户 id =1 发送链接http://localhost/users-data/activate1。然后我可以取最后一个对应于 id 的 url 编号,并在我的激活脚本中设置该用户的状态。

你能告诉我我做错了什么吗?

这是我的注册操作

public function registerAction()
{
    // action body
    $request = $this->getRequest();
    $form    = new Application_Form_UsersData();

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($request->getPost())) {
            $comment = new Application_Model_UsersData($form->getValues());
            $mapper  = new Application_Model_UsersDataMapper();
            $mapper->save($comment);

            // send email
            $id = $comment -> getId();
            $formValues = $this->_request->getParams();
            $mail = new Application_Model_Mail();
            $mail->sendActivationEmail($formValues['email'], $id,$formValues['name']);
            $this->_redirect('/users-data/regsuccess'); 

        }
    }

    $this->view->form = $form;
}

这是我的 Application_Model_Mail

class Application_Model_Mail
{

    public function sendActivationEmail($email, $id,$name)
    {
        require_once('Zend/Mail/Transport/Smtp.php');
        require_once 'Zend/Mail.php';


        $config = array('auth' => 'login',
                'username' => '*******@gmail.com',
                'password' => '******',
                'port'     => '587',
                'ssl' => 'tls');
        $tr = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
        Zend_Mail::setDefaultTransport($tr);

        $mail = new Zend_Mail();

        $mail->setBodyText('Please click the following link to activate your account '
                  . '<a http://localhost/users-data/activate/.php?id='.$id.'>'.$id.'</a>')
                ->setFrom('admin@yourwebsite.com', 'Website Name Admin')
                ->addTo($email, $name)
                ->setSubject('Registration Success at Website Name')
                ->send($tr);

    }

}

【问题讨论】:

    标签: php zend-framework hyperlink zend-mail email-confirmation


    【解决方案1】:

    您从$request 转到$_request。后者没有在任何地方定义,所以它的值为空。

    【讨论】:

    • 感谢您的回答。但看起来至少 $email 和 $name 正在设置,一旦它们正确出现在电子邮件中。无论如何我如何定义 $_request ?
    【解决方案2】:

    您创建的 HTML 链接不正确:

    '<a http://localhost/users-data/activate/.php?id='.$id.'>'
    

    在 HTML 中,有效链接是(注意 href 属性):

    <a href="http://...">...</a>
    

    所以你的脚本应该像这样构造链接:

    '<a href="http://localhost/users-data/activate/.php?id='.$id.'">'
    

    另外,我想http://localhost/users-data/activate/.php 不是您想要的:您的脚本可能在php 扩展名之前有一个名称。

    【讨论】:

    • 感谢您的回答!即便如此,我的电子邮件显示如下: 请点击以下链接激活您的帐户 localhost/users-data/activate/.php?id=></a> 我真正想要的只是该链接,例如发送给用户 id =1 的链接 localhost/users-data/activate1。然后我可以取最后一个 url,它应该对应于 id,并在我的激活脚本中设置这个用户的状态。
    【解决方案3】:

    试试这个

    $mail->setBodyHtml("Please click the following link to activate your".
    "account<a href='http://localhost/users-data/activate.php?id=$id'>$id</a>")
    

    【讨论】:

    • 感谢您的回答。这样做,链接就不会出现在电子邮件中......
    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2018-11-04
    • 2016-07-31
    相关资源
    最近更新 更多