2015-05-29 16 views
6

Tôi biết điều này là ở nơi khác trên trang web nhưng vì lý do nào đó, câu trả lời tôi tìm thấy ở đây vẫn chưa giúp tôi vẫn không thể làm việc sau khi thử những thứ khác nhau cả ngày . Mục tiêu của tôi là có mã xác nhận được gửi đến email mà người dùng nhập. Tôi chỉ biết một lượng nhỏ PHP và theo một hướng dẫn cho hệ thống đăng nhập/đăng ký nhưng tôi khá chắc chắn rằng php đang làm việc tốt và vấn đề nằm trong sendmail.ini và php.ini của tôi. Đây là tôi sendmail.iniCách cấu hình xampp để gửi email

; configuration for fake sendmail 

; if this file doesn't exist, sendmail.exe will look for the settings in 
; the registry, under HKLM\Software\Sendmail 

[sendmail] 

; you must change mail.mydomain.com to your smtp server, 
; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup) 
; emails delivered via IIS's pickup directory cause sendmail to 
; run quicker, but you won't get error messages back to the calling 
; application. 

smtp_server=smtp.gmail.com 

; smtp port (normally 25) 

smtp_port=587 

; SMTPS (SSL) support 
; auto = use SSL for port 465, otherwise try to use TLS 
; ssl = alway use SSL 
; tls = always use TLS 
; none = never try to use SSL 

smtp_ssl=auto 

; the default domain for this server will be read from the registry 
; this will be appended to email addresses when one isn't provided 
; if you want to override the value in the registry, uncomment and modify 

;default_domain=mydomain.com 

; log smtp errors to error.log (defaults to same directory as sendmail.exe) 
; uncomment to enable logging 

error_logfile=error.log 

; create debug log as debug.log (defaults to same directory as sendmail.exe) 
; uncomment to enable debugging 

debug_logfile=debug.log 

; if your smtp server requires authentication, modify the following two lines 

auth_username= [email protected] 
auth_password= mygmailpass 

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines. do not enable unless it is required. 

pop3_server= 
pop3_username= 
pop3_password= 

; force the sender to always be the following email address 
; this will only affect the "MAIL FROM" command, it won't modify 
; the "From: " header of the message content 

[email protected] 

; force the sender to always be the following email address 
; this will only affect the "RCTP TO" command, it won't modify 
; the "To: " header of the message content 

force_recipient= 

; sendmail will use your hostname and your default_domain in the ehlo/helo 
; smtp greeting. you can manually set the ehlo/helo name if required 

hostname= 

Và tôi những phần tôi đã chỉnh sửa trong php.ini của tôi

extension=php_openssl.dll 

[mail function] 
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury 
SMTP =smtp.gmail.com 
smtp_port =587 

; For Win32 only. 
; http://php.net/sendmail-from 
sendmail_from = [email protected] 


sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" 

;sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" 

; Force the addition of the specified parameters to be passed as extra parameters 
; to the sendmail binary. These parameters will always replace the value of 
; the 5th parameter to mail(), even in safe mode. 
;mail.force_extra_parameters = 

và mã PHP của tôi trong trường hợp nó là cần thiết

<?php 
session_start(); 
include('configdb.php'); 
if(isset($_POST['submit'])) 
{ 
//whether the username is blank 
if($_POST['username'] == '') 
{ 
    $_SESSION['error']['username'] = "User Name is required."; 
} 
//whether the email is blank 
if($_POST['email'] == '') 
{ 
    $_SESSION['error']['email'] = "E-mail is required."; 
} 
else 
{ 
    //whether the email format is correct 
    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email'])) 
    { 
    //if it has the correct format whether the email has already exist 
    $email= $_POST['email']; 
    $sql1 = "SELECT * FROM user WHERE email = '$email'"; 
    $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error()); 
    if (mysqli_num_rows($result1) > 0) 
      { 
    $_SESSION['error']['email'] = "This Email is already used."; 
    } 
    } 
    else 
    { 
    //this error will set if the email format is not correct 
    $_SESSION['error']['email'] = "Your email is not valid."; 
    } 
} 
//whether the password is blank 
if($_POST['password'] == '') 
{ 
    $_SESSION['error']['password'] = "Password is required."; 
} 
//if the error exist, we will go to registration form 
if(isset($_SESSION['error'])) 
{ 
    header("Location: index.php"); 
    exit; 
} 
else 
{ 
    $username = $_POST['username']; 
    $email = $_POST['email']; 
    $password = $_POST['password']; 
    $com_code = md5(uniqid(rand())); 

    $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')"; 
    $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error()); 

    if($result2) 
    { 
    $to = $email; 
    $subject = "Confirmation for $username"; 
    $header = "Confirmation from"; 
    $message = "Please click the link below to verify and activate your account. rn"; 
    $message .= "http://www.yourname.com/confirm.php?passkey=$com_code"; 

    $sentmail = mail($to,$subject,$message,$header); 

    if($sentmail) 
      { 
    echo "Your confirmation link has been sent to your e-mail address."; 
    } 
    else 
     { 
    echo "Error while sending confirmation link to your e-mail address"; 
    } 
    } 
} 
} 
?> 

Giống như tôi nói rằng tôi đã làm việc này một thời gian và tôi dường như không thể hình dung ra được, tôi cho rằng đó là một cái gì đó rất đơn giản nhưng vì tôi vẫn đang học tôi không thấy nó. Cảm ơn vì sự giúp đỡ!

Trả lời

5

nếu bạn đang gửi thư thông qua máy chủ địa phương mà bạn cần phải cấu hình

c:\xampp\sendmail\sendmail.ini (gửi qua Gmail) và C:\xampp\php\php.ini.

trong php.ini

tìm extension=php_openssl.dll và loại bỏ các dấu chấm phẩy từ đầu dòng đó. Vì vậy, điều này cho phép bạn gửi thư từ localhost.

và sau đó tìm mail function và thay đổi dưới đây cài đặt

SMTP=smtp.gmail.com 
smtp_port=587 
sendmail_from = [email protected] 
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" 

trong sendmail.ini (đường dẫn - C:\xampp\sendmail\sendmail.ini)

Thay đổi này

[sendmail]  
smtp_server=smtp.gmail.com 
smtp_port=587 
error_logfile=error.log 
debug_logfile=debug.log 
auth_username=GMail [email protected]//your gmail ID 
auth_password=GMail PASSWORD // Your gmail password 
force_sender=GMail [email protected] //your gmail ID 

Thay đổi GMail ID @ gmail. com, GMail PA SSWORD, GMail [email protected] ở mã trên.

Cuối cùng

bình luận sau dòng nếu có một sendmail_path: sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" trong php.ini tập tin.

+0

Cảm ơn bạn đã trả lời! Nhưng nó vẫn không hoạt động vì một số lý do: /. Người dùng được thêm vào cơ sở dữ liệu mà tôi có nhưng tôi vẫn nhận được câu lệnh khác từ mã php của tôi có nội dung "Lỗi khi gửi liên kết xác nhận đến địa chỉ email của bạn" và email không được gửi và nó không nằm trong thư rác của tôi thư mục. –

+1

Có một hướng dẫn bị thiếu: Google hiện chặn loại kết nối này theo mặc định và để bỏ chặn nó, bạn cần truy cập cài đặt bảo mật của GMail và bật kết nối "kém an toàn". Dưới đây là hướng dẫn từ Google: https://support.google.com/accounts/answer/6010255?hl=vi – Ynhockey

6

Bạn chỉ có thể định cấu hình C: \ xampp \ php \ php.ini và c: \ xampp \ sendmail \ sendmail.ini để gmail gửi thư.

Trong file php.ini tìm [email chức năng] và thay đổi sau

SMTP=smtp.gmail.com 
 
smtp_port=587 
 
sendmail_from = [email protected] 
 
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Bây giờ mở C: \ xampp \ sendmail \ sendmail.ini. Thay thế tất cả các mã hiện có trong sendmail.ini với mã sau đây

[sendmail] 
 
    
 
smtp_server=smtp.gmail.com 
 
smtp_port=587 
 
error_logfile=error.log 
 
debug_logfile=debug.log 
 
[email protected] 
 
auth_password=your-gmail-id-password 
 
[email protected]

Bây giờ bạn đã làm !! tạo file php với chức năng mail và gửi mail từ localhost.

+0

Có, nó phù hợp với tôi. cảm ơn @sumit Tôi đã cứu ngày của tôi. –

Các vấn đề liên quan