2013-09-05 3 views
0

PHP를 사용하여 메일을 보내려고합니다. wampserver2.2를 사용하고 있습니다.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"; 
     } 
    } 
?> 

문제는 내가 그것을 사용하려고 할 때 다음과 같은 오류를 얻고있다 : 그리고 내 PHP 버전은 여기에 5.4.3

내가 노력 코드입니다. 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

나도 없다'펀치 : – Rob

답변

1

따라서 Gmail에는 SMTP 인증이 필요합니다. Mail은 해당 인터페이스를 제공하지 않습니다.

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

하나를 던져 mail''ini_set' 나)에 초점을 할 때 너무 잘 작동하지 않습니다 – Fluffeh

+0

때때로 SO에 대한 질문에 대답하는 것은 군비 경쟁처럼 나를 이길'PDOException' –

+0

을 와인을 마시 며 담배를 피운다. 어쨌든 중대한 대답 :) – Fluffeh

2

PHP의 mail() 기능을 사용하면 연결을 시도하는 것과 같은 공공 서버에 연결할 때 일반적으로 오류가 발생 SMTP 서버에 인증을 사용하지 않는 것입니다. 당신은 당신이 autheticate 수있는 PHPMailer 같은 라이브러리를 사용할 수도 있습니다.

관련 문제