2013-09-05 120 views
0

我想用php发送邮件。我正在使用wampserver2.2。而我的PHP版本是5.4.3无法使用PHP发送邮件

这里是我试过的代码:

<?php 
if(isset($_POST['submit'])) 
{ 
    $YourName = mysql_real_escape_string($_POST['YourName']); 
    $From = mysql_real_escape_string($_POST['Email']); 
    $Subject = mysql_real_escape_string($_POST['Subject']); 
    $Message = mysql_real_escape_string($_POST['message']); 
    $myEmailAdr = "[email protected]"; 

     if (preg_match('/^[a-zA-Z][a-zA-Z0-9_-][email protected][a-zA-Z]+[.]{1}[a-zA-Z]+$/', $From)) 
     { 
      $headers = 'From: '.$From."\r\n".'Reply-To: '.$From; 
      $Message = str_replace("\n.", "\n..", $Message); 

      try 
      { 
        ini_set("SMTP","ssl:smtp.gmail.com"); 
        ini_set("smtp_port","465"); 
        $send = mail($myEmailAdr, $Subject, $Message, $headers); // line 24 
      } 
      catch(PDOException $e) 
      { 
        $error = $e->getMessage(); 
        echo $error; 
      } 
     } 
     else 
     { 
      echo "Error in Mail Address Format"; 
     } 
    } 
?> 

的问题是我收到以下错误,当我尝试使用它。第24行表示邮件功能。

Warning: mail(): Failed to connect to mailserver at "ssl:smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\UMVSS_Website\ContactScript.php on line 24

+2

既不''ini_set'或'mail'抛出一个'PDOException' – Rob

回答

1

因此,GMail需要SMTP认证。邮件不提供该界面。

查看PHPMailer。下面是一个example for connecting to GMail

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>PHPMailer - GMail SMTP test</title> 
</head> 
<body> 
<?php 

//SMTP needs accurate times, and the PHP time zone MUST be set 
//This should be done in your php.ini, but this is how to do it if you don't have access to that 
date_default_timezone_set('Etc/UTC'); 

require '../class.phpmailer.php'; 

//Create a new PHPMailer instance 
$mail = new PHPMailer(); 
//Tell PHPMailer to use SMTP 
$mail->IsSMTP(); 
//Enable SMTP debugging 
// 0 = off (for production use) 
// 1 = client messages 
// 2 = client and server messages 
$mail->SMTPDebug = 2; 
//Ask for HTML-friendly debug output 
$mail->Debugoutput = 'html'; 
//Set the hostname of the mail server 
$mail->Host  = 'smtp.gmail.com'; 
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
$mail->Port  = 587; 
//Set the encryption system to use - ssl (deprecated) or tls 
$mail->SMTPSecure = 'tls'; 
//Whether to use SMTP authentication 
$mail->SMTPAuth = true; 
//Username to use for SMTP authentication - use full email address for gmail 
$mail->Username = "[email protected]"; 
//Password to use for SMTP authentication 
$mail->Password = "yourpassword"; 
//Set who the message is to be sent from 
$mail->SetFrom('[email protected]', 'First Last'); 
//Set an alternative reply-to address 
$mail->AddReplyTo('[email protected]','First Last'); 
//Set who the message is to be sent to 
$mail->AddAddress('[email protected]', 'John Doe'); 
//Set the subject line 
$mail->Subject = 'PHPMailer GMail SMTP test'; 
//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body 
$mail->MsgHTML(file_get_contents('contents.html'), dirname(__FILE__)); 
//Replace the plain text body with one created manually 
$mail->AltBody = 'This is a plain-text message body'; 
//Attach an image file 
$mail->AddAttachment('images/phpmailer_mini.gif'); 

//Send the message, check for errors 
if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
?> 
</body> 
</html> 
+0

+1打我冲一下:) – Fluffeh

+0

有时回答SO上的问题就像是一场军备竞赛; –

+0

这个功能不太好,啜饮葡萄酒和吸烟香烟嘿嘿。无论如何,很好的答案:) – Fluffeh

2

的PHP mail()功能不使用身份验证的SMTP服务器,它连接到公共服务器,比如你试图连接到一个当通常会导致一个错误。您可能需要使用像PHPMailer这样的库,您可以对其进行验证。