是否有承載這個頁面的機器上運行的郵件服務器?最有可能不是因爲你展示的代碼應該工作。
現在有兩種選擇:安裝郵件服務器或使用額外的PHP庫來解決此問題。我不會發布關於如何安裝郵件服務器的教程,所以只需Google!
對於PHP庫部分,你可以使用PHPMailer。此類包含通過特定服務器發送郵件的其他設置。首先從GitHub下載所需的文件(在右側,點擊「下載zip」)https://github.com/PHPMailer/PHPMailer。解壓縮包並將文件複製到您的工作目錄。代碼可能是這樣的:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$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 = '[email protected]'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
發送郵件的最簡單方法是使用Gmail帳戶或其他東西。這些設置可以從字面上隨處找到。例如:http://phpmailer.worxware.com/?pg=examplebgmail