2020年用PHP 7.2设置SMTP邮件-2020年PHP邮件



我想知道如何在2020年在php 7.2中设置自动SMTP邮件系统。我知道mail()函数,但这是在初始设置之后。

我在stackoverflow上看到了其他关于它的问题,但我发布了这个问题,因为我只发现过时的问题,比如20112012,从那时起,PHP在安全方面和其他方面发生了很大的变化。

以下是我尝试过的:

根据我的发现,我应该更改php.ini文件中的ini_set(),但那里没有ini_set()函数。我所做的是像别人告诉我的那样,将smtp更改为smtp=my-mail-server-of-choice-here,并将smtp_port更改为smtp_port=587

此外,我应该更新sendmail文件夹中的sendmail.ini,但(猜怎么着(sendmail folder不存在->这也意味着sendmail.exesendmail.ini也不存在

我也收到这个错误mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first

这是文件:

$to = 'my-users-mail@some-random.mail';
$subject = "HTML email";
$message = "Hi Bob!";
$headers = "MIME-Version: 1.0"."rn";
$headers .= "Content-type:text/html;charset=UTF-8"."rn";
$headers .= 'From: my-mail@gmail.com'."rn";
mail($to,$subject,$message,$headers);

(根据我所读到的(应该可以从ini_set()中修复,而php.ini文件中的任何地方都不存在-我想只有过时的解决方案可用

现代标准php 7.2的实现方式是什么?

顺便说一句,我在localhost上使用XAMPP v3.2.4(我将在生产中转到WAMPP(,我使用gmail作为我的邮件服务

你对"现代的";思考新的做事方式实际上是使用扩展、框架等。-可重用代码是这个快速扩展的it世界中的单词

早在90年代或80年代,人们就没有如此广泛的互联网,如果你想(比方说(在两台计算机之间建立连接(比如美国国家航空航天局或其他什么(,你必须从头开始编写自己的协议(POP或IMAP(,并花数周甚至数月的时间进行编程。

现在是2020年。我们有很多可重复使用的代码,存储库,开源软件等等

当然,你可以在一周内用php编写自己的身份验证,也可以简单地下载

Net_Socket(我有1.2.2(->https://pear.php.net/package/Net_Socket/download/Net_SMTP(我有1.9.2(->https://pear.php.net/package/Net_SMTP/download/Mail_extension(我有1.4.1版本(->https://pear.php.net/package/Mail/download/

用7-zip提取所有内容,并像这样构建

您的主文件夹

.Mail-1.4.1
>Mail.php
>Mail
> some default files(dont touch these)
> Net(here you paste files from Net_SMTP and Net_Socket - they should be named SMTP.php and Socket.php)
. sendmail.php

在sendmail.php中,您可以这样写:

//Make sure you made your folder/file structure like you should
require_once "./Mail-1.4.1/Mail.php";
$host = "your-mail-server-of-choice-here";
$port = "465";
$username = "your mail or username";
$password = "your password";
//setting up smtp connection
$smtp = Mail::factory(
'smtp',
array (
'host' => $host,
'port' => $port,
//you don't need this if u are using mail server that doesn't need authentication
'auth' => true,
'username' => $username,
'password' => $password
)
);
$from = "your-mail-here";
$to = "recepient-mail-here";
$subject = "Ay bro!";
$body = "Your message here!";
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
exit( "Error happened :( -->".$mail->getMessage() );
}

这很容易,对吧?如果我们按照你的方式行事,你会花很多时间和眼泪(是的,眼泪(建立所有这些联系和东西,确保安全等等。我希望你对这个结果感到满意!

最新更新