douliang9057 2014-11-07 17:57
浏览 6

PHPmailer仅在浏览器指向URL时发送

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来