Actions

Difference between revisions of "Validate an e-mail address"

From Joomla! Documentation

m
m
Line 2: Line 2:
 
<source lang="php">
 
<source lang="php">
 
$mail =& JFactory::getMailer();
 
$mail =& JFactory::getMailer();
if ($mail->ValidateAddress($mailaddress)) echo 'Valid';
+
if (!$mail->ValidateAddress($mailaddress)) echo 'Invalid';
 
</source >
 
</source >
 
This is implicitly done for every JMAIL function that sets an address (such as addRecipient, addBCC, ..) and though there are other potential errors besides address validating, it most probably is something you want catch too.
 
This is implicitly done for every JMAIL function that sets an address (such as addRecipient, addBCC, ..) and though there are other potential errors besides address validating, it most probably is something you want catch too.
 
<source lang="php">
 
<source lang="php">
 
$mail =& JFactory::getMailer();
 
$mail =& JFactory::getMailer();
if ($mail->addRecipient($mailaddress)) echo 'Valid';
+
if ($mail->addRecipient($data['EMAIL'])->IsError()) echo 'Invalid';
 
</source >
 
</source >
  
 
[[Category:JMail]] [[Category:Developers]] [[Category:CodeExample]] [[Category:MethodExample]]
 
[[Category:JMail]] [[Category:Developers]] [[Category:CodeExample]] [[Category:MethodExample]]

Revision as of 10:59, 21 November 2012

If you want to validate an e-mail address in your code and you will use JMAIL anyway, you can use the function from its parent class phpmailer.

$mail   =& JFactory::getMailer();
if (!$mail->ValidateAddress($mailaddress)) echo 'Invalid';

This is implicitly done for every JMAIL function that sets an address (such as addRecipient, addBCC, ..) and though there are other potential errors besides address validating, it most probably is something you want catch too.

$mail   =& JFactory::getMailer();
if ($mail->addRecipient($data['EMAIL'])->IsError()) echo 'Invalid';