0

这是我的代码:

<?php

function smtp_mail($from, $to, $subject, $message, $headers="", $params=array()) {
    $errno = "";
    $errstr = "";
    $smtpConnect = fsockopen("mail2.allramhosting.com", "25", $errno, $errstr, 20);


    if (!empty($smtpConnect)) {

        if ($params['use_html']) {
            $extra_headers .= "MIME-Version: 1.0\nContent-Type: text/html; charset=\"utf-8\"\nContent-Transfer-Encoding: 7bit\n";
        }
        $headers = "To: " . $to . "\nFrom: " . $from . "\nSubject: " . $subject . "\nDate: " . date('r') . "\n" . $extra_headers . $headers;

        if (substr($headers, -1) == "\n") $headers = substr($headers, 0, -1); // Our last character should not be newline

        //fputs($smtpConnect,"AUTH LOGIN" . $newLine);
        //$smtpResponse = fgets($smtpConnect, 515);
        //fputs($smtpConnect, base64_encode($username) . $newLine);
        //$smtpResponse = fgets($smtpConnect, 515);
        //fputs($smtpConnect, base64_encode($password) . $newLine);
        //$smtpResponse = fgets($smtpConnect, 515);
        fputs($smtpConnect, "HELO networkssms.com\r\n");
        fputs($smtpConnect, "MAIL FROM: " . $from . "\r\n");
        fputs($smtpConnect, "RCPT TO: " . $to . "\r\n");
        fputs($smtpConnect, "DATA\r\n");
        fputs($smtpConnect, $headers . "\r\n\r\n");
        fputs($smtpConnect, $message . "\r\n.\r\n"); // send message and finish
        fputs($smtpConnect,"QUIT" . $newLine);
        fclose($smtpConnect);
        return true;
    }
    return false;
}




$params['use_html'] = "yes";

$user_subject = "Ticket - Budget Notification";
$user_part = '<html><body>';
$user_part .= '<div style="background-color: #EEEEEE;border-bottom: 1px solid #DCDCDC;padding: 10px 10px 20px;width: 500px; float:left;font-size:12px;font-weight:bold;">';
$user_part1 = '<div style="width:500px;">';
$user_part1 .= '<div style="width:500px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">';
$user_part1 .= '<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;">';
$user_part1 .= '</div></div>';
$user_part1 .= '<div style="width:450px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc; height:auto; float:left;font-size:12px;margin-left:50px; font-weight:bold;">';
$user_part3 = '</div></div></div>';
$user_part3 .= '</body></html>';
$user_center_part1 = '';    
$user_center_part = 'The following Tickets are reached its budget ,';   
$user_center_part2='<div style="float:left;width:auto;color:#000000;font-size:12px;margin-left:75px; font-weight:bold; height:auto; margin-top:10px;">Ticket #10 reached 100% of its budget</div>';
$user_to = 'galtech.staffs@gmail.com';
$user_center_part1 = 'Dear Admin ,';
$user_message = $user_part.$user_center_part1.$user_part1.$user_center_part.$user_center_part2.$user_part3; 

if (!smtp_mail("noreply@networkssms.com", $user_to, $user_subject, $user_message,"",$params)){
    mail($user_to, $user_subject, $user_subject . "\n" . substr($user_message, 0, 158), "From: noreply@networkssms.com\nX-PHP-Script: 1");
    //mail("galtech.staffs@gmail.com", $user_subject, $user_message ,"From: noreply@networkssms.com\nX-PHP-Script: 1");
}


?>

我的帐户中没有收到任何邮件。如果我将其作为没有 html 内容的普通邮件或简单邮件提供,则它可以正常工作,但不适用于 html 内容。

4

2 回答 2

1

我建议您使用经过良好测试和记录的库,例如 SwiftMailer,而不是使用您自己的自定义复杂函数来发送 html 电子邮件。

非常 易于使用。对于您的示例,它可以是:

$user_subject = "Ticket - Budget Notification";
/* ... */
$user_message = $user_part.$user_center_part1.$user_part1.$user_center_part.$user_center_part2.$user_part3;

// then, send the mail
require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail2.allramhosting.com', 25);

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create the message
$message = Swift_Message::newInstance()
  ->setSubject($user_subject)
  ->setFrom('noreply@networkssms.com')
  ->setTo($user_to)
  ->addPart($user_message, 'text/html');

// Send the message
$result = $mailer->send($message);
于 2012-04-10T11:47:59.667 回答
0

您是否尝试过使用 PEAR 的 Mail_MIME 之类的东西?

您可以查看http://pear.php.net/manual/en/package.mail.mail-mime.example.php#15034并使用您的 smtp 服务器信息($smtpinfo 数组)尝试该示例。

于 2012-04-10T11:03:17.463 回答