从SMTP服务器使用PHP发送电子邮件

Bor*_*lis 121 php email smtp

$from = "someonelse@example.com";
$headers = "From:" . $from;
echo mail ("borutflis1@gmail.com" ,"testmailfunction" , "Oj",$headers);
Run Code Online (Sandbox Code Playgroud)

我在用PHP发送电子邮件时遇到问题.我收到一个错误:SMTP server response: 530 SMTP authentication is required.

我的印象是你可以发送没有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
Run Code Online (Sandbox Code Playgroud)

这是php.ini文件中的设置.我该如何设置SMTP?是否有任何SMTP服务器不需要验证或我必须自己设置服务器?

Ivo*_*ira 157

当您通过需要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();
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到更多关于PHPMailer的信息:https://github.com/PHPMailer/PHPMailer

  • 为phpMailer +1 - 它是替代PHP内置`mail()`函数的理智人. (21认同)
  • 值得注意的是,对于这个答案的绊脚石,PHPMailer也内置到WordPress中,可以使用`'phpmailer_init'`动作钩子进行配置.这是为SMTP邮件或Amazon SES(支持SMTP连接)设置WordPress的便捷方式. (8认同)
  • @Luka是的。根据他们的[许可证文件](https://github.com/PHPMailer/PHPMailer/blob/master/LICENSE),PHPMailer使用LGPL 2.1许可证,该许可证允许商业用途。 (2认同)

Dip*_*mar 49

<?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....&lt;BR/>";
?>
Run Code Online (Sandbox Code Playgroud)

要么

继续阅读

  • 这是godaddy`php mail()`函数问题的最佳答案 - 2017 - 不应该下载PHPMailer或其他一些第三方资源 - 谢谢 (5认同)
  • "READ ON"链接已断开 (4认同)

txy*_*oji 44

对于Unix用户,mail()实际上是使用Sendmail命令发送电子邮件.您可以更改环境,而不是修改应用程序.msmtp是一个具有Sendmail兼容CLI语法的SMTP客户端,这意味着它可以代替Sendmail使用.它只需要对你的php.ini进行一些小改动.

sendmail_path = "/usr/bin/msmtp -C /path/to/your/config -t"
Run Code Online (Sandbox Code Playgroud)

那么即使是lowly mail()函数也可以使用SMTP优点.如果您尝试将现有应用程序连接到sendgrid或mandrill等邮件服务而不修改应用程序,那么它非常有用.

  • 在我的Docker容器上为不使用邮件库的应用程序实现此功能. (3认同)

Bol*_*lli 15

以下是使用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>';
}
Run Code Online (Sandbox Code Playgroud)

如果您使用Gmail SMTP,请记住在Gmail帐户中启用SMTP,然后在设置下


Geo*_*tov 12

问题是PHP mail()函数的功能非常有限.有几种方法可以从PHP发送邮件.

  1. mail()在您的系统上使用SMTP服务器.您可以在Windows上使用至少两台服务器:hMailServerxmail.我花了几个小时来配置和获取它们.在我看来,第一个更简单.目前,hMailServer正在开发Windows 7 x64.
  2. mail()在Linux上使用远程或虚拟机上的SMTP服务器.当然,像Gmail这样的真实邮件服务不允许在没有任何凭据或密钥的情况下直接连接.您可以设置虚拟机或使用位于LAN中的虚拟机.大多数Linux发行版都有开箱即用的邮件服务器.配置它并享受乐趣.我在Debian 7上使用默认的exim4来监听它的LAN接口.
  3. 邮件库使用直接连接.Libs更容易设置.我使用了SwiftMailer,它完美地从Gmail帐户发送邮件.我认为PHPMailer也很不错.

不管你有什么选择,我建议你使用一些抽象层.您可以在运行Windows的开发机器上使用PHP库,只需mail()在使用Linux的生产机器上运行即可.抽象层允许您根据运行应用程序的系统交换邮件驱动程序.MyMailer使用抽象send()方法创建抽象类或接口.继承两个类MyPhpMailerMySwiftMailer.send()以适当的方式实施方法.


Pek*_*ica 10

有些SMTP服务器无需身份验证即可运行,但如果服务器需要身份验证,则无法绕过该身份验证.

PHP的内置邮件功能非常有限 - 只能在WIndows中指定SMTP服务器.在*nix上,mail()将使用操作系统的二进制文件.

如果要将电子邮件发送到网络上的任意SMTP服务器,请考虑使用类似SwiftMailer的库.这样您就可以使用Google Mail的外发服务器.