PHPmailer internal functions for validation/sanitazion vs external
email, forms, html, php, phpmailer
Solution
Well, you can't use the code you've posted; it's an internal function in phpMailer that does stuff to the internal variables in the phpMailer object.
However, you can call the email address validation routine that is used by this code `ValidateAddress()` because it's a `public static` function.
So anywhere in your code, you should be able to call something like this:
$valid = phpMailer::ValidateAddress($email_address);
...and `$valid` will be set to `true` or `false` depending on whether its a valid address.
However, the point of the code in the question is that this is the code where PHPMailer sets its email addresses internally, and it is using `ValidateAddress()` itself. This means that phpMailer is already doing the validation whenever you give it an email address. You don't really need to validate it youself first because phpMailer will do it for you anyway.
What you do need to do is trap any errors that phpMailer might throw back at you as a result of valdation errors. By default it simply returns `false` if functions fail, so you can check for that. Alternatively, you can switch on its exceptions mode and use `try...catch` to pick up exceptions that it throws.
Either way, the point is that phpMailer is designed to be secure by default; it won't accept bad email addresses or other invalid data. So your question isn't really necessary; just give phpMailer the data, and it will do the validation for you.
(That's not to say phpMailer is perfect code; it's possible that someone may find a security hole in it, just as it's possible with any other software, but as long as you make sure to keep up-to-date with patch releases, that shouldn't be an issue)
Problem
After searching for about a week for a good answer I realized that I had to ask for some help. I have been battling if I should use PHP-mailer or Swiftmailer for a week, I decided on PHP-mailer since I feel the documentation is more comprehensive and there are more examples out there. After looking in to the code, I found that PHP-mailer have many built in sanitation/validation functions. I wonder if any one seen any drawbacks or holes in them? How would you use them? To give the question a little more substance, do I wonder how you would validate/sanitize the following form data in a process.php file, using phpmailer. ``` <form class="form-horizontal" id="contact-form-info" action="func/mail/mail-info.php" method="post"> <fieldset> <div class="control-group"> <label class="control-label" for="name">Namn</label> <div class="controls"> <input type="text" class="input-xlarge" name="name" id="name" maxlength="50" placeholder="Namn *"/> </div> </div> <div class="control-group"> <label class="control-label" for="foretag">Företag</label> <div class="controls"> <input type="text" class="input-xlarge" name="foretag" id="foretag" maxlength="50" placeholder="Företag" /> </div> </div> <div class="control-group"> <label class="control-label" for="email">E-post</label> <div class="controls"> <input type="text" class="input-xlarge" name="email" id="email" placeholder="E-post *"/> </div> </div> <div class="control-group"> <label class="control-label" for="subject">Ämne</label> <div class="controls"> <input type="text" class="input-xlarge" name="subject" id="subject" maxlength="5000" placeholder="Ämne *"/> </div> </div> <div class="control-group"> <label class="control-label" for="message">Meddelande</label> <div class="controls"> <textarea class="input-xlarge" name="message" placeholder="Meddelande *" id="message" rows="3"></textarea> </div> </div> <div class="form-actions"> <input id="submit" name="submit" type="submit" value="Skicka" class="btn btn-custom btn-large"></input> <input type="reset" class="btn" value="återställ"></input> </div> </fieldset> </form> ``` Examples of phpmailer internal validation line 338-384 ``` * Adds an address to one of the recipient arrays * Addresses that have been added already return false, but do not throw exceptions * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo' * @param string $address The email address to send to * @param string $name * @throws phpmailerException * @return boolean true on success, false if address already used or invalid in some way * @access protected */ protected function AddAnAddress($kind, $address, $name = '') { if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) { $this->SetError($this->Lang('Invalid recipient array').': '.$kind); if ($this->exceptions) { throw new phpmailerException('Invalid recipient array: ' . $kind); } if ($this->SMTPDebug) { $this->edebug($this->Lang('Invalid recipient array').': '.$kind); } return false; } $address = trim($address); $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim if (!$this->ValidateAddress($address)) { $this->SetError($this->Lang('invalid_address').': '. $address); if ($this->exceptions) { throw new phpmailerException($this->Lang('invalid_address').': '.$address); } if ($this->SMTPDebug) { $this->edebug($this->Lang('invalid_address').': '.$address); } return false; } if ($kind != 'Reply-To') { if (!isset($this->all_recipients[strtolower($address)])) { array_push($this->$kind, array($address, $name)); $this->all_recipients[strtolower($address)] = true; return true; } } else { if (!array_key_exists(strtolower($address), $this->ReplyTo)) { $this->ReplyTo[strtolower($address)] = array($address, $name); return true; } } return false; } ```