简体   繁体   English

php从localhost发送邮件

[英]php send mail from localhost

I am new at php. 我是php新手。 I was trying to send mail from php using this code. 我试图使用此代码从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);

?>

I have change settings in php.ini 我在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

& in sendmail.ini &在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

Now code runs successfully but I am not getting any mail. 现在代码运行成功,但我没有收到任何邮件。

You must change the php.ini file: 您必须更改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

It won't work if localhost is set, for that reason change to your mail server. 如果设置了localhost,它将无法工作,因此更改为您的邮件服务器。

You won't have SMTP server installed by default so you can't send emails from localhost directly. 默认情况下,您不会安装SMTP服务器,因此您无法直接从localhost发送电子邮件。 Either you can set up SMTP server on local or use third party SMTP servers. 您可以在本地设置SMTP服务器或使用第三方SMTP服务器。 Have a look at http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp which gives you insight about how to send mail from localhost using third party SMTP server. 请查看http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp ,它可以让您深入了解如何使用第三方SMTP服务器从localhost发送邮件。

该功能在本地主机上不起作用,因为locahost不能用作SMTP服务器,将内容上传到安装了SMTP的有效服务器,然后进行邮件呼叫。

Your server does not have local mailserver. 您的服务器没有本地邮件服务器。

There are few solutions: 解决方案很少:

  • Install local mail server if you have sufficient rights 如果您有足够的权限,请安装本地邮件服务器
  • Change your PHP settings to use other mail server (other open mailserver or auth-based ones like Gmail, Yahoo etc) 更改您的PHP设置以使用其他邮件服务器(其他打开的邮件服务器或基于身份验证的邮件服务器,如Gmail,Yahoo等)
  • Use one of available mail libraries which supports IMAP / POP3 to handle mail sending. 使用支持IMAP / POP3的可用邮件库之一来处理邮件发送。 SwiftMailer or Pear Mail are one of most commonly used. SwiftMailer或Pear Mail是最常用的一种。

Try to set below things in your php.ini, 尝试在php.ini中设置以下内容,

  1. "SMTP" to "mail.YourDomain.com" “SMTP”到“mail.YourDomain.com”
  2. "smtp_port" to "25" “smtp_port”改为“25”

OR you can set this option using php script also, 或者您也可以使用php脚本设置此选项,

// Please specify your Mail Server oR other mail server which you are going to use.(eg gmail, yahoo) //请指定您要使用的邮件服务器或其他邮件服务器。(例如gmail,yahoo)

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

// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports. //请指定SMTP号码25和8889是有效的SMTP端口。

ini_set("smtp_port","25");

Here's the link that gives me the answer: 这是给我答案的链接:

Install the "fake sendmail for windows". 安装“假的sendmail for Windows”。 If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip 如果您不使用XAMPP,可以在此处下载: http//glob.com.au/sendmail/sendmail.zip

Modify the php.ini file to use it (commented out the other lines): 修改php.ini文件以使用它(注释掉其他行):

mail function 邮件功能

For Win32 only. 仅适用于Win32。

SMTP = smtp.gmail.com

smtp_port = 25

For Win32 only. 仅适用于Win32。

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

For Unix only. 仅适用于Unix。 You may supply arguments as well 你也可以提供论据

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

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

(ignore the "Unix only" bit, since we actually are using sendmail) (忽略“仅限Unix”位,因为我们实际上使用的是sendmail)

You then have to configure the "sendmail.ini" file in the directory where sendmail was installed: 然后,您必须在安装sendmail的目录中配置“sendmail.ini”文件:

sendmail 发送邮件

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

I had this issue the past weeks, on my centos boxes, sharing this for others also having issues with mail() in php not sending... This resolved the issue for all of my mail() php scripts. 过去几周,我在我的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