how to send message with validation link

Notice: This thread is very old.
alnux
Member | 139
+
0
-

Hi there,

Im trying to create a link validation to email address send. I did this, works, but i think there is a route option that i cant find

$values['link_validation'] = $presenter->getHttpRequest()->getUrl()->baseUrl.$presenter->name."/validate/"."<hash of validation>";
duke
Member | 650
+
0
-

Just use something like this in your email message template:

<a n:href="//Auth:validateEmail hash => $hash">click on this link to validate your email address</a>
alnux
Member | 139
+
0
-

thanks, not crossed my mind to do it in the template

duke
Member | 650
+
0
-

You don't have to use templates if you don't want. Then you can use method link() of Presenter directly:

$validationLinkUrl = $presenter->link('//Auth:validateEmail', ['hash' => $hash]);
alnux
Member | 139
+
0
-

By the way i had a exeption with the first option

Latte\CompileException

Unknown attribute n:href in

Last edited by alnux (2015-02-17 19:54)

duke
Member | 650
+
0
-

It seems your template is not correctly configured (doesn't contain link macro).
The easiest way to get configured template object is:

$template = clone $presenter->getTemplate();
alnux
Member | 139
+
0
-

Now i have the next error

Undefined variable: _control
<p>Para activar tu cuenta por favor haz click en <a href="<?php echo Latte\Runtime\Filters::escapeHtml($_control->link("//Homepage:validate", array('hash' => $auth_key)), ENT_COMPAT) ?>

php code is

$emailLatte = clone $presenter->getTemplate();
$emailmessage->htmlBody($emailLatte->getLatte()->renderToString($htmlfile,$this->params));

my latte template

<html>
	<head>
	    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	    <title>Registro</title>
	</head>
	<body>
	    <p>Hola {$username},</p>

	    <p>Para activar tu cuenta por favor haz click en <a href="{$auth_key_link}">este enlace</a> .</p>
	    <p>Para activar tu cuenta por favor haz click en <a n:href="//Homepage:validate hash => $auth_key">este enlace</a> .</p>

	    <p>Gracias y bienvenido a {$site_title}</p>
	</body>
</html>

Last edited by alnux (2015-02-17 22:05)

duke
Member | 650
+
0
-

Problem is with this line:

$emailmessage->htmlBody($emailLatte->getLatte()->renderToString($htmlfile,$this->params));

… because you completely exclude parameters already defined in $emailLatte (including _control).

Correct approach should be:

// obtain configured instance of Latte Template:
$template = clone $presenter->getTemplate();
// set custom source file (without this, it would use presenter's current action's template file)
$template->setFile(__DIR__ . '/../../templates/yourEmailMessageTemplateFile.latte');
// add your custom parameters at once:
$template->setParameters($this->params);
// or one by one:
$template->foo = 'foo';
$template->bar = 'bar';
// create the email message object:
$message = new Nette\Mail\Message;
// optionally add message object to template to allow its manipulation from within the template file:
$template->mail = $message;
// prepare the email message:
$message->setFrom($fromEmail/* , $fromName*/)
	->addTo($targetEmail/* , $targetName*/)
	->setHtmlBody((string) $template)
	->setSubject($subject); // optional, will be set by setHtmlBody if <title>Subject</title> is found in the template file
// send the message:
$mailer->send($message);

Last edited by duke (2015-02-18 01:42)

alnux
Member | 139
+
0
-

A lot of thanks duke