I'm trying to send an email (using PHPMailer) and insert data into a MySQL table in the same PHP script. The script is called from an Ajax request and the insert functions work fine, but the script will not send email. However, the email will be sent if I execute the script by pointing my browser to the PHP file's URL.
I've used PHPMailer once before and used my previous code as a template for my current project. Here I have omitted the code that inserts data into the table because I know that runs successfully. Below is the code for PHPMailer:
require_once 'lib/phpmailer/PHPMailerAutoload.php';
$m = new PHPMailer(true);
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 3;
$m->Host = 'smtp.myDomain.com';
$m->Username = 'email@myDomain.com'; //username of email account used to send
$m->Password = 'myPW'; //PW of email account used to send
$m->SMTPSecure = 'tls';
$m->Port = '587';
$m->From = 'email@myDomain.com';
$m->FromName = 'From Name';
$m->addReplyTo('email@myDomain.com','Reply address');
$m->addAddress('address@domain.com');
$m->isHTML(true); //Set body to HTML
$m->Subject = 'My Subject';
//HTML Body
$m->Body = '<p>Hi [Name goes here],</p>
<p>Test body</p></br></br>
';
//If client doesn't support HTML email will display AltBody as plain text
$m->AltBody = 'Test Alt Body.';
//Send email
if($m->send()) {
echo 'Email sent.';
} else {
echo $m->ErrorInfo;
}
Is there any part of this that is incorrect? I'm new to PHP and am puzzled why the script would be executed successfully when done through the URL and unsuccessfully when called from the Ajax request.