首页 > 解决方案 > 2020 年使用 PHP 7.2 设置 SMTP 邮件 - 2020 年 PHP 邮件

问题描述

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

我已经在 stackoverflow 上看到了关于它的其他问题,但是我发布了这个问题,因为我只发现了像2011 年2012 年这样的过时问题,并且 PHP从安全方面和其他方面从那时起发生了很大变化。

这是我尝试过的:

根据我的发现,我应该ini_set()php.ini文件中进行更改,但那里没有任何ini_set()功能。我所做的是我改变smtp了我被告知的样子smtp=my-mail-server-of-choice-heresmtp_portsmtp_port=587

另外,我应该sendmail.inisendmail文件夹中更新,但(猜猜是什么)sendmail folder不存在->这也意味着sendmail.exe也不sendmail.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"."\r\n";
$headers .= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers .= 'From: my-mail@gmail.com'."\r\n";

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

哪个(从我读到的)应该是可修复的,文件中不存在ini_set()哪个- 我猜 bc 只有过时的解决方案可用php.ini

什么是现代标准,php 7.2,这样做实际上可以工作并且安全的方式是什么?

顺便说一句,我正在使用XAMPP v3.2.4(我将WAMPP在生产中使用)localhost并且我正在使用gmail我的邮件服务

标签: phpemailsmtp

解决方案


你对“现代”思维有一个坏主意。新的做事方式实际上是使用扩展、框架等...... -可重用代码是这个快速扩展的 IT 世界中的词

回到像 90 年代或 80 年代那样,人们根本没有如此广泛或可用的互联网,如果你想(比如说)在两台计算机之间建立连接(想想 NASA 或其他东西),你必须编写自己的协议(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/下载/ 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() );
}

那很容易吧?如果我们按照您的方式行事,您将花费大量时间和眼泪(是的眼泪)来建立所有这些联系和东西并使其安全等。我希望您对这个结果感到满意!


推荐阅读