带有SMTP的AJAX jQuery JSON PHPMailer

问题描述 投票:2回答:1

下面的代码运行良好,直到托管公司阻止PHPMailer。

他们告诉我可以使用SMTP instate

我编辑了下面的代码,但它仍然无效

session_cache_limiter('nocache');

header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');

include 'php-mailer/class.phpmailer.php';

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.website.com';
$mail->Port = 25;
$mail->SMTPSecure = '';
$mail->Username = '[email protected]';
$mail->Password = 'websitepass';

$to = '[email protected]';
$subject = $_POST['subject'];

if($to) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $fields = array(
        0 => array(
            'text' => 'Name',
            'val' => $_POST['name']
        ),
        1 => array(
            'text' => 'Email address',
            'val' => $_POST['email']
        ),
        2 => array(
            'text' => 'Phone',
            'val' => $_POST['phone']
        ),
        3 => array(
            'text' => 'Campany',
            'val' => $_POST['cname']
        ),
        4 => array(
            'text' => 'Country',
            'val' => $_POST['ccountry']
        ),
        5 => array(
            'text' => 'Service',
            'val' => $_POST['servicetype']
        ),
        6 => array(
            'text' => 'Message',
            'val' => $_POST['message']
        )
    );
    $message = "";

    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }

    $headers = '';

    $headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
    $headers .= "Reply-To: " .  $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    if (mail($to, $subject, $message, $headers)){
        $arrResult = array ('response'=>'success');
    } else{
        $arrResult = array ('response'=>'error');
    }

    echo json_encode($arrResult);

} else {
    $arrResult = array ('response'=>'error');
    echo json_encode($arrResult);
}
php ajax phpmailer
1个回答
0
投票

我想你应该改变这个

由于mail是发送邮件的php函数,而不是PHPMailer函数。

if (mail($to, $subject, $message, $headers)){
    $arrResult = array ('response'=>'success');
} else{
    $arrResult = array ('response'=>'error');
}

对此:

if(!$mail->send()) {
    $arrResult = array ('response'=>'success');
} else {
    $arrResult = array ('response'=>'error');
}

确保已设置所有标头和参数,否则将无法正常工作。

我在wiki:https://github.com/PHPMailer/PHPMailer/wiki/Tutorial的教程中找到了这个send()函数

© www.soinside.com 2019 - 2024. All rights reserved.