0

我的简单代码如下:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: info@mail.com \r\n";
$headers .= 'Bcc: test@mail.com' . "\r\n";

$subject = "Information";

if (mail($email, $subject, $message, $headers)) {
    $mail_status = "success";
} else {
    $mail_status = "fail";
}

if ($mail_status == "success") echo '{"status":"success"}';

如何支持 SMTP 支持?

4

2 回答 2

1

正如您在标签中提到的 PHPMailer 。您可以在此过程中使用 PHPMailer 类。

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, 
    //gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

来源:http ://phpmailer.worxware.com/index.php?pg=examplebsmtp

于 2013-05-13T07:37:46.757 回答
0

SMTP 服务器必须在服务器上进行配置,但使用 PHP,这是一种编程语言。要求服务器管理员为您提供 SMTP 详细信息。

SMTP 信息可以设置在php.ini或通过ini_set()

例子:

ini_set("SMTP","smtp.example.com" );
ini_set('sendmail_from', 'user@example.com'); 

顺便说一句,如果你需要 SMTP Authentication,mail()不支持它。尝试使用其他 PHP 邮件库,例如 SwiftMailer 或 PHPMailer。

于 2013-05-13T07:32:51.680 回答