簡體   English   中英

php從localhost發送郵件

[英]php send mail from localhost

我是php新手。 我試圖使用此代碼從php發送郵件。

<?php

    $to      = 'sohil@gmail.com';
    $subject = 'The subject';
    $message = 'hello';
    $headers = 'From: sohil@yahoo.in' . "\r\n" .
        'Reply-To: receiver@yahoo.in' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

?>

我在php.ini中更改了設置

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = sohil@gmail.com

&在sendmail.ini中

# A freemail service example
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from sohil@gmail.com
auth on
user sohil@gmail.com
password xxxxxxxxx

# Set a default account
account default : Gmail

現在代碼運行成功,但我沒有收到任何郵件。

您必須更改php.ini文件:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

如果設置了localhost,它將無法工作,因此更改為您的郵件服務器。

默認情況下,您不會安裝SMTP服務器,因此您無法直接從localhost發送電子郵件。 您可以在本地設置SMTP服務器或使用第三方SMTP服務器。 請查看http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp ,它可以讓您深入了解如何使用第三方SMTP服務器從localhost發送郵件。

該功能在本地主機上不起作用,因為locahost不能用作SMTP服務器,將內容上傳到安裝了SMTP的有效服務器,然后進行郵件呼叫。

您的服務器沒有本地郵件服務器。

解決方案很少:

  • 如果您有足夠的權限,請安裝本地郵件服務器
  • 更改您的PHP設置以使用其他郵件服務器(其他打開的郵件服務器或基於身份驗證的郵件服務器,如Gmail,Yahoo等)
  • 使用支持IMAP / POP3的可用郵件庫之一來處理郵件發送。 SwiftMailer或Pear Mail是最常用的一種。

嘗試在php.ini中設置以下內容,

  1. “SMTP”到“mail.YourDomain.com”
  2. “smtp_port”改為“25”

或者您也可以使用php腳本設置此選項,

//請指定您要使用的郵件服務器或其他郵件服務器。(例如gmail,yahoo)

ini_set("SMTP","mail.YourDomain.com");

//請指定SMTP號碼25和8889是有效的SMTP端口。

ini_set("smtp_port","25");

這是給我答案的鏈接:

安裝“假的sendmail for Windows”。 如果您不使用XAMPP,可以在此處下載: http//glob.com.au/sendmail/sendmail.zip

修改php.ini文件以使用它(注釋掉其他行):

郵件功能

僅適用於Win32。

SMTP = smtp.gmail.com

smtp_port = 25

僅適用於Win32。

sendmail_from = <e-mail username>@gmail.com

僅適用於Unix。 你也可以提供論據

(default: "sendmail -t -i").

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

(忽略“僅限Unix”位,因為我們實際上使用的是sendmail)

然后,您必須在安裝sendmail的目錄中配置“sendmail.ini”文件:

發送郵件

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>@gmail.com

過去幾周,我在我的centos盒子里遇到了這個問題,為其他人分享了這個問題,而且在php中沒有發送郵件問題...這解決了我所有mail()php腳本的問題。

// Enable the sendmail in selinux
setsebool -P httpd_can_sendmail 1

// Add the following to /etc/postfix/main.cf
relayhost = smtp.server.com

// Then from command line
service postfix restart

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM