PHP+Ubuntu 使用 gmail 形式发送电子邮件 localhost

2022-01-25 00:00:00 email ubuntu localhost php postfix-mta

我已经搜索了几个关于此的帖子,但没有运气.每个人都在使用后缀.但是当我浏览文本时https://help.ubuntu.com/community/Postfix

I have searched several posts on this but no luck. Everyone is using postfix. But when I gone through the text on https://help.ubuntu.com/community/Postfix

什么是邮件传输代理换句话说,它是一个邮件服务器而不是一个邮件客户端,例如 Thunderbird、Evolution、Outlook、Eudora 或基于 Web 的电子邮件服务,例如 Yahoo、GMail、Hotmail、Earthlink、Comcast、SBCGlobal.net、ATT.net 等......如果你在一家公司工作名为 Acme 并拥有 acme.com,您可以为您的员工提供电子邮件地址@acme.com.员工可以发送和接收电子邮件通过您的计算机,但并非没有您的计算机运行所有时间.如果您的所有电子邮件地址都在一个域中 (@gmail.com,@yahoo.com)您不拥有(您不拥有 Google)或不托管(acme.com) 那么你根本不需要这个.

What is a Mail Transfer Agent In other words, it's a mail server not a mail client like Thunderbird, Evolution, Outlook, Eudora, or a web-based email service like Yahoo, GMail, Hotmail, Earthlink, Comcast, SBCGlobal.net, ATT.net etc.... If you worked for a company named Acme and owned acme.com, you could provide your employees with email addresses @acme.com. Employees could send and receive email through your computer, but not without your computer running all the time. If all your email addresses are at a domain (@gmail.com, @yahoo.com) you do not own (you don't own Google) or do not host (acme.com) then you do not need this at all.

正如最后一行所说,您不能将它用于 gmail 或 yahoo 以使其从 localhost 工作..!

As the last line says You cannot us it for gmail or yahoo to make it work from localhost..!

谁能告诉我如何使用 gmail SMTP 在 localhost 上配置邮件服务器?我正在使用 Ubuntu 14.

Can anyone tell me how can I configure mail server on localhost using gmail SMTP? I am using Ubuntu 14.

在没有一个对我有用之前我尝试过的链接.在测试下面列出的链接期间没有错误或警告

Links I have tried before NONE of them worked for me. No error or warnings during testing of below listed links

https://askubuntu.com/questions/314664/sending-php-mail-from-本地主机https://askubuntu.com/questions/228938/how-can-i-configure-postfix-to-send-all-email-through-my-gmail-accounthttps://easyengine.io/tutorials/linux/ubuntu-postfix-gmail-smtp/https://easyengine.io/tutorials/mail/postfix-debugging/

推荐答案

请按照以下步骤在 Ubuntu/Linux 上通过 gmail<从 localhost 发送邮件/strong> :-

Please do following steps to send mail from localhost on Ubuntu/Linux through gmail :-

为此,您需要在 Linux/Ubuntu 服务器上安装 msmtp.

For that you need to install msmtp on Linux/Ubuntu server.

Gmail 使用 https://(它是超文本安全的),因此您需要安装 ca-certificates

~$ sudo apt-get install msmtp ca-certificates

安装 msmtp 包需要几秒钟的时间.

It will take few seconds to install msmtp package.

现在您必须使用 gedit 编辑器创建配置文件(msmtprc).

Now you have to create configuration file(msmtprc) using , gedit editor.

~$ sudo gedit /etc/msmtprc

现在您必须将以下代码复制并粘贴到 gedit 中(您使用上述命令创建的文件)

Now you have to copy and paste following code in gedit (file you created with above command)

defaults
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.gmail.com
port 587
auth on
user MY_GMAIL_ID@gmail.com
password MY_GMAIL_PASSSWORD
from MY_GMAIL_ID@gmail.com
logfile /var/log/msmtp.log

不要忘记将 MY_GMAIL_ID 替换为您的gmail id",并将 MY_GMAIL_PASSSWORD 替换为您的gmail 密码" 在上面的代码行中.

Don't forget to replace MY_GMAIL_ID with your "gmail id" and MY_GMAIL_PASSSWORD with your "gmail password" in above lines of code.

现在创建 msmtp.log

~$ sudo touch /var/log/msmtp.log

你必须让任何人都可以阅读这个文件

You have to make this file readable by anyone with

~$ sudo chmod 0644 /etc/msmtprc

现在使用

~$ sudo chmod 0777 /var/log/msmtp.log

您的 gmail SMTP 配置现已准备就绪.现在发送一封测试电子邮件为

Your configuration for gmail's SMTP is now ready. Now send one test email as

~$ echo -e "Subject: Test Mail

This is my first test email." |msmtp --debug --from=default -t MY_GMAIL_ID@gmail.com

请检查您的 Gmail 收件箱.

Please check your Gmail inbox.

现在,如果您想从 localhost 使用 php 发送电子邮件,请按照以下说明操作:-

Now if you want to send email with php from localhost please follow below instructions:-

打开和编辑php.ini文件

Open and edit php.ini file

~$ sudo gedit /etc/php/7.0/apache2/php.ini

您必须在 php.ini 文件中设置 sendmail_path.

You have to set sendmail_path in your php.ini file.

使用

~$ which msmtp 

你会得到 /usr/bin/msmtp 这样的.

php.ini中搜索sendmail_path并编辑如下

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/bin/msmtp -t

请仔细检查第 3 行 sendmail_path 之前没有分号.

Please check 3rd line carefully there is no semicolon before sendmail_path.

现在保存并退出 gedit.现在是时候重新启动你的 apache

Now save and exit from gedit. Now it's time to restart your apache

~$ sudo /etc/init.d/apache2 restart

现在从 http://in2.php 创建一个带有邮件功能的 php 文件.net/manual/en/function.mail.php.

做测试和享受!

相关文章