使用$ _SERVER时mail()出现问题

时间:2016-06-08 05:23:48

标签: php email

我有一个使用Muse的网站,我使用了联系表单小部件,它生成一个PHP脚本,用以下函数发送表单以生成正文消息:

function get_email_body($subject, $heading, $fields, $resources) {
    $message = '';
    $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    $message .= '<html xmlns="http://www.w3.org/1999/xhtml">';
    $message .= '<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><title>' . encode_for_form($subject) . '</title></head>';
    $message .= '<body style="background-color: #ffffff; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: 18px; font-family: helvetica, arial, verdana, sans-serif;">';
    $message .= '<h2 style="background-color: #eeeeee;">' . $heading . '</h2>';
    $message .= '<table cellspacing="0" cellpadding="0" width="100%" style="background-color: #ffffff;">'; 

    $sorted_fields = array();

    foreach ($fields as $field => $properties) {
        // Skip reCAPTCHA from email submission
        if ('recaptcha' == $properties['type'])
            continue;

        array_push($sorted_fields, array('field' => $field, 'properties' => $properties));
    }

    // sort fields
    usort($sorted_fields, 'field_comparer');

    foreach ($sorted_fields as $field_wrapper)
        $message .= '<tr><td valign="top" style="background-color: #ffffff;"><b>' . encode_for_form($field_wrapper['properties']['label']) . ':</b></td><td>' . get_form_field_value($field_wrapper['field'], $field_wrapper['properties'], $resources, true) . '</td></tr>';

    $message .= '</table>';
    $message .= '<br/><br/>';
    $message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME'])) . '</div>';
    $message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_by'], encode_for_form($_SERVER['REMOTE_ADDR'])) . '</div>';
    $message .= '</body></html>';

    return cleanup_message($message);
}

此功能使主持人不会发送电子邮件,但如果我使用$_SERVER['SERVER_NAME']$_SERVER['REMOTE_ADDR']对这些行发表评论,则会毫无问题地发送邮件。

//$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME'])) . '</div>';
//$message .= '<div style="background-color: #eeeeee; font-size: 10px; line-height: 11px;">' . sprintf($resources['submitted_by'], encode_for_form($_SERVER['REMOTE_ADDR'])) . '</div>';

该脚本还会生成以下标题行:

function get_email_headers($to_email, $form_email) {
    $headers = 'From: ' . $to_email . PHP_EOL;
    $headers .= 'Reply-To: ' . $form_email . PHP_EOL;
    //$headers .= 'X-Mailer: Adobe Muse CC 2015.1.1.343 with PHP' . PHP_EOL;
    $headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;

    return $headers;
}

我真的在Muse论坛等中搜索有关它的信息,但我想知道为什么这些$_SERVER['SERVER_NAME']$_SERVER['REMOTE_ADDR']会导致发送崩溃。主持人不会抛出任何错误或警告信息。

非常感谢。

回答评论:

function cleanup_message($message) {
    $message = wordwrap($message, 70, "\r\n");
    return $message;
}
function encode_for_form($text) {
    $text = stripslashes($text);
    return htmlentities($text, ENT_QUOTES, 'UTF-8');// need ENT_QUOTES or webpro.js jQuery.parseJSON fails
}

sprintf($resources['submitted_from'], encode_for_form($_SERVER['SERVER_NAME']))打印一个字符串,例如&#34;从网站anubbe.com&#34;发送。它来自一个阵列。

1 个答案:

答案 0 :(得分:0)

请不要使用任何依赖关系点击此链接: - https://github.com/PHPMailer/PHPMailer

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;