php 如何从本地主机发送smtp邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32677350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:06:36  来源:igfitidea点击:

how to send smtp mail from localhost

phpemailsmtp

提问by Usman Ali Siddiqui Sabzwari

I always get this message when trying to send email from my local host.

尝试从本地主机发送电子邮件时,我总是收到此消息。

SMTP Error: Could not connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

SMTP 错误:无法连接到 SMTP 主机。无法发送消息。邮件程序错误:SMTP 错误:无法连接到 SMTP 主机。

below is my code: please help

以下是我的代码:请帮忙

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("class.phpmailer.php");

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: [email protected]
// pass: password
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "Nov112014"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("[email protected]", "Usman Ali Siddiqui");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website Etutionhub!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

采纳答案by urfusion

  1. Open the php.ini. For XAMPP, it is located in C:\XAMPP\php\php.ini. Find out if you are using WAMP or LAMP server.
    Note: Make a backup of php.inifile.

  2. Search [mail function] in the php.ini file.

    You can find like below.
    [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 = postmaster@localhost
    

    Change the localhost to the smtp server name of your ISP. No need to change the smtp_port. Leave it as 25. Change sendmail_fromfrom postmaster@localhostto your domain email address which will be used as from address.

    So for me, it will become like this.

    [mail function]
    ; For Win32 only.
    SMTP = smtp.example.com
    smtp_port = 25
    ; For Win32 only.
    sendmail_from = [email protected]
    
  3. Restart the XAMPP or WAMP(apache server) so that changes will start working.

  4. Now try to send the mail using the mail()function.

    mail("[email protected]","Success","Great, Localhost Mail works");
    
  1. 打开php.ini. 对于 XAMPP,它位于C:\XAMPP\php\php.ini. 确定您使用的是 WAMP 还是 LAMP 服务器。
    注意:备份php.ini文件。

  2. 在php.ini文件中搜索【邮件功能】。

    You can find like below.
    [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 = postmaster@localhost
    

    将 localhost 更改为 ISP 的 smtp 服务器名称。无需更改smtp_port. 把它作为25.更改sendmail_frompostmaster@localhost哪个将被用作从地址域的电子邮件地址。

    所以对我来说,它会变成这样。

    [mail function]
    ; For Win32 only.
    SMTP = smtp.example.com
    smtp_port = 25
    ; For Win32 only.
    sendmail_from = [email protected]
    
  3. 重新启动 XAMPP 或 WAMP(apache 服务器),以便更改将开始工作。

  4. 现在尝试使用该mail()函数发送邮件。

    mail("[email protected]","Success","Great, Localhost Mail works");
    

Mail will be sent to [email protected] from the localhost with Subject line "Success" and body "Great, Localhost Mail works".

邮件将从本地主机发送到 [email protected],主题行“成功”和正文“很棒,本地主机邮件有效”。

回答by Samundra Khatri

You can add this line
$mail->SMTPSecure = "ssl";

你可以添加这一行
$mail->SMTPSecure = "ssl";

after

$mail->SMTPAuth = true;