[英]Sending email with PHP from an SMTP server
$from = "someonelse@example.com";
$headers = "From:" . $from;
echo mail ("borutflis1@gmail.com" ,"testmailfunction" , "Oj",$headers);
我在 PHP 中发送 email 时遇到问题。我收到错误消息: SMTP server response: 530 SMTP authentication is required
。
我的印象是您可以发送 email 而无需 SMTP 进行验证。 我知道这封邮件可能会被过滤掉,但现在这并不重要。
[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 = someonelse@example.com
这是php.ini
文件中的设置。 我应该如何设置SMTP? 有没有SMTP服务器不需要验证或者必须自己架设服务器?
当您通过需要 SMTP 身份验证的服务器发送电子邮件时,您确实需要指定它,并设置主机、用户名和密码(如果不是默认端口,还可以设置端口 - 25)。
例如,我通常使用与以下设置类似的 PHPMailer:
$mail = new PHPMailer();
// Settings
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
您可以在此处找到有关 PHPMailer 的更多信息: https : //github.com/PHPMailer/PHPMailer
<?php
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "YOURMAIL@gmail.com");
$message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail@address.com";
$headers = "From: YOURMAIL@gmail.com";
mail("Sending@provider.com", "Testing", $message, $headers);
echo "Check your email now....<BR/>";
?>
或者,有关更多详细信息, 请继续阅读。
这是一种使用 PHP PEAR 实现的方法
// Pear Mail Library
require_once "Mail.php";
$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // change to address
$subject = 'Insert subject here'; // subject of mail
$body = "Hello world! this is the content of the email"; //content of mail
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'your@gmail.com', //your gmail account
'password' => 'snip' // your password
));
// Send the mail
$mail = $smtp->send($to, $headers, $body);
//check mail sent or not
if (PEAR::isError($mail)) {
echo '<p>'.$mail->getMessage().'</p>';
} else {
echo '<p>Message successfully sent!</p>';
}
如果您使用 Gmail SMTP 请记住在您的 Gmail 帐户中启用 SMTP,在设置下
编辑:如果你在 debian/ubuntu 上找不到 Mail.php,你可以安装 php-pear
sudo apt install php-pear
然后安装邮件扩展:
sudo pear install mail
sudo pear install Net_SMTP
sudo pear install Auth_SASL
sudo pear install mail_mime
然后你应该可以通过简单的require_once "Mail.php"
加载它,否则它位于: /usr/share/php/Mail.php
问题是 PHP mail()
函数的功能非常有限。 有几种方法可以从 PHP 发送邮件。
mail()
在您的系统上使用 SMTP 服务器。 您至少可以在 Windows 上使用两个服务器: hMailServer和xmail 。 我花了几个小时来配置和安装它们。 在我看来,第一个更简单。 现在,hMailServer 正在 Windows 7 x64 上运行。mail()
在 Linux 的远程或虚拟机上使用 SMTP 服务器。 当然,像 Gmail 这样的真实邮件服务不允许在没有任何凭据或密钥的情况下直接连接。 您可以设置虚拟机或使用位于 LAN 中的虚拟机。 大多数 linux 发行版都有开箱即用的邮件服务器。 配置它并玩得开心。 我在 Debian 7 上使用默认的 exim4 来监听它的 LAN 接口。 无论您的选择是什么,我都建议您使用一些抽象层。 您可以在运行 Windows 的开发机器上使用 PHP 库,并在使用 Linux 的生产机器上使用mail()
函数。 抽象层允许您根据应用程序运行的系统交换邮件驱动程序。 使用抽象的send()
方法创建抽象的MyMailer
类或接口。 继承两个类MyPhpMailer
和MySwiftMailer
。 以适当的方式实现send()
方法。
有一些 SMTP 服务器无需身份验证即可工作,但如果服务器需要身份验证,则无法绕过。
PHP 的内置邮件功能非常有限 - 只能在 WINdows 中指定 SMTP 服务器。 在 *nix 上, mail()
将使用操作系统的二进制文件。
如果您想将电子邮件发送到网络上的任意 SMTP 服务器,请考虑使用像SwiftMailer这样的库。 例如,这将使您能够使用 Google Mail 的外发服务器。
如果您在 Linux 上托管WordPress站点并具有服务器访问权限,您可以通过安装 msmtp 来避免一些麻烦,它允许您从标准的 PHP mail() 函数通过SMTP发送。 msmtp 是 postfix 的一个更简单的替代方案,它需要更多的配置。
以下是步骤:
安装 msmtp
sudo apt-get install msmtp-mta ca-certificates
创建一个新的配置文件:
sudo nano /etc/msmtprc
...具有以下配置信息:
# Set defaults.
defaults
# Enable or disable TLS/SSL encryption.
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Set up a default account's settings.
account default
host <smtp.example.net>
port 587
auth on
user <username@example.net>
password <password>
from <address-to-receive-bounces@example.net>
syslog LOG_MAIL
您需要替换由“<”和“>”中的所有内容表示的配置数据(包括,删除这些)。 对于主机/用户名/密码,使用您的普通凭据通过邮件提供商发送邮件。
告诉 PHP 使用它
sudo nano /etc/php5/apache2/php.ini
添加这一行:
sendmail_path = /usr/bin/msmtp -t
完整的文档可以在这里找到:
对于另一种方法,您可以采用这样的文件:
From: Sunday <sunday@gmail.com>
To: Monday <monday@gmail.com>
Subject: Day
Tuesday Wednesday
并像这样发送:
<?php
$a1 = ['monday@gmail.com'];
$r1 = fopen('a.txt', 'r');
$r2 = curl_init('smtps://smtp.gmail.com');
curl_setopt($r2, CURLOPT_MAIL_RCPT, $a1);
curl_setopt($r2, CURLOPT_NETRC, true);
curl_setopt($r2, CURLOPT_READDATA, $r1);
curl_setopt($r2, CURLOPT_UPLOAD, true);
curl_exec($r2);
如果有人需要,我为 PHP 创建了一个简单的轻量级 SMTP 电子邮件发送器。 这是网址:
https://github.com/jerryurenaa/EZMAIL
它在生产环境和开发环境中都经过了测试。
我知道这是一个老问题,但它仍然有效,我看到的所有答案都显示了基本身份验证,这已被弃用。 下面是一个示例,展示了如何使用带有 XOAUTH2 身份验证的 PHPMailer 通过 Google 的 Gmail 服务器进行发送:
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\OAuth;
//Alias the League Google OAuth2 provider class
use League\OAuth2\Client\Provider\Google;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Load dependencies from composer
//If this causes an error, run 'composer install'
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number:
// - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or
// - 587 for SMTP+STARTTLS
$mail->Port = 465;
//Set the encryption mechanism to use:
// - SMTPS (implicit TLS on port 465) or
// - STARTTLS (explicit TLS on port 587)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType to use XOAUTH2
$mail->AuthType = 'XOAUTH2';
//Fill in authentication details here
//Either the gmail account owner, or the user that gave consent
$email = 'someone@gmail.com';
$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
//Obtained by configuring and running get_oauth_token.php
//after setting up an app in Google Developer Console.
$refreshToken = 'RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0';
//Create a new OAuth2 provider instance
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
//Pass the OAuth provider instance to PHPMailer
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'userName' => $email,
]
)
);
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail->setFrom($email, 'First Last');
//Set who the message is to be sent to
$mail->addAddress('someone@gmail.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->msgHTML(file_get_contents('contentsutf8.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
参考: PHPMailer 示例文件夹
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.