使用核心 php mail() 通过 gmail SMTP 发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10028428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using core php mail() to send via gmail SMTP
提问by Darshita
Is it possible to send mail in core php via gmail smtp without using any external class?
是否可以在不使用任何外部类的情况下通过 gmail smtp 在核心 php 中发送邮件?
采纳答案by Pierre-Olivier
I don't think it is possible because you need to perform an authentification. Also, you need to connect via an SSL socket, I don't know if the stock mail()function support this.
我认为这是不可能的,因为您需要进行身份验证。另外,你需要通过SSL套接字连接,我不知道股票mail()功能是否支持这个。
If you are willing to use the Pear Mailpackage, you might want to take a look at this:
如果你愿意使用Pear Mail包,你可能想看看这个:
回答by gavanon
There's a lot of miscommunication about this. It is 100% possible to send emails using gmail via PHP's simple "mail()" command. Andit is 100% easy.
关于这一点有很多误解。通过 PHP 的简单“mail()”命令使用 gmail 发送电子邮件是 100% 可能的。而且它是100%容易。
Install SSMTP:
安装 SSMTP:
sudo apt-get install ssmtp
Edit its settings file:
编辑其设置文件:
sudo nano /etc/ssmtp/ssmtp.conf
Inside, make it similar to this, but with your own credentials:
在里面,使它与此类似,但使用您自己的凭据:
mailhub=smtp.gmail.com:587
[email protected]
AuthPass=password
UseSTARTTLS=YES
# You can only do this if you've verified your domain with Gmail.
# If you haven't, delete, or add a # before this
hostname=yourwebsite.com
FromLineOverride=YES
Lastly, open your php.ini, and search for sendmail_pathand use this value:
最后,打开你的 php.ini,搜索sendmail_path并使用这个值:
sendmail_path = /usr/sbin/ssmtp -t
That's it! Test it out in your PHP, with the simple 1-line mail function:
就是这样!在您的 PHP 中使用简单的1 行邮件功能对其进行测试:
mail('[email protected]', 'Subject', 'Message', 'From: Your name <[email protected]>');
Update on Gmail Security
Gmail 安全更新
Gmail now blocks this by default. You can still do this by visiting: http://www.google.com/settings/security/lesssecureapps
Gmail 现在默认阻止此操作。您仍然可以通过访问:http: //www.google.com/settings/security/lesssecureapps来执行此操作
Turn this feature ON.
开启此功能开启。
回答by mensi
It is possible, although you have to modify php.ini settings, see the PHP manual. You can modify php.ini settings at runtime with ini_set
这是可能的,尽管您必须修改 php.ini 设置,请参阅PHP 手册。您可以在运行时使用ini_set修改 php.ini 设置
回答by Dave Goodchild
If you have access to edit the php.inithen you can do something like this:
如果您有权编辑,php.ini那么您可以执行以下操作:
[mail function]
SMTP = ssl://smtp.gmail.com
smtp_port = 465
username = [email protected]
password = myemailpassword
sendmail_from = [email protected]
Alternatively you can do:
或者你可以这样做:
<?php
ini_set( 'smtp_port', 465 );
//etc
回答by anubhav
you can do this by PHPmailer Library it already having gmail.php file .
你可以通过已经有 gmail.php 文件的 PHPmailer 库来做到这一点。
just open and place your detail in that file , you can also use the similar code in your file .
只需打开并将您的详细信息放在该文件中,您也可以在文件中使用类似的代码。
You must make setting in your gmail account setting to allow smtp mailing
您必须在您的 gmail 帐户设置中进行设置以允许 smtp 邮件

