php – I keep getting a redeclared function error when I try to run my HTML doc (valid_email)
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Asked
Viewed
5 times
<?php
require_once('C:xamppphppearPEAR.php');
function send_email($to, $from, $subject, $body, $is_body_html = false) {
require_once("Mail.php");
function valid_email($email)
{
if(!valid_email($to))
{
throw new Exception('This To address is invalid: ' . htmlspecialchars($to));
}
if(!valid_email($from))
{
throw new Exception('This From address is invalid: ' . htmlspecialchars($from));
}
$emailObjects = Mail_RFC822::parseAddressList($email);
if(PEAR::isError($emailObjects)) {return false;}
$emailObject = $emailObject[0];
$email = $emailObject->mailbox . '@' . $emailObject->host;
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
return false;
} else{
return true;
}
}
$smtp = array();
$smtp['host'] = 'ssl://smtp.gmail.com' ;
$smtp['port'] = 465;
$smtp['auth'] = true;
$smtp['username'] = 'dtestnasr123@gmail.com';
$smtp['password'] = 'PasswordTest123!';
$mailer = Mail::factory('smtp', $smtp);
if(PEAR::isError($mailer))
{
throw new Exception('Could not create mailer');
}
$recipients = array();
$recipients[] = $to;
$headers = array();
// $headers['From'] = $from;
$headers['From'] = "coolemailthatisunique@gmail.com";
$headers['To'] = $to;
$headers['Subject'] = $subject;
// var_dump($headers);
if($is_body_html){
$headers['Content-type'] = 'text/html';
}
$result = $mailer->send($recipients, $headers, $body);
// if(PEAR::isError($result))
// {
// throw new Exception('Error sending email: ' . htmlspecialchars($result->getMessage)() );
// }
}
?>
Dmon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
lang-php

Leave an answer