php 如何使用phpmailer从本地主机发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28522542/
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:54:54  来源:igfitidea点击:

How to send e-mail from localhost with phpmailer?

phphtmlemaillocalhost

提问by Henrikus Anthony

okay, so I already try it for many times. The results was not error but I didn't receive any e-mail in my inbox or spam folder

好的,所以我已经尝试过很多次了。结果没有错误,但我的收件箱或垃圾邮件文件夹中没有收到任何电子邮件

here is my mail.php

这是我的mail.php

    <?php


require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
//$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // SMTP server
//IsSMTP(); // send via SMTP
$mail->SMTPDebug = true;
$mail->IsSMTP();
$mail->Host     = "smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer   = "gmail";
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587;   


$mail->Username = "[email protected]"; // 
$mail->Password = "******"; // SMTP password
$webmaster_email = "[email protected]"; //Reply to this email ID
$email = "[email protected]"; // Recipients email ID
$name = "Hendrikus Anthony"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Anthony";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Anthony");
$mail->WordWrap = 50; // set word wrap

$mail->IsHTML(true); // send as HTML
$mail->Subject = "Ini adalah Email HTML";
$mail->Body = "Ini adalah email contoh"; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

please someone, I really need help. do I need a hosting? or there are something wrong with my syntax? whether sendmail.ini and php.ini affect the mail.php?

请某人,我真的需要帮助。我需要托管吗?还是我的语法有问题?sendmail.ini 和php.ini 是否影响mail.php?

回答by Dhaval Chheda

Here is my solution which I found out from couple of articles.

这是我从几篇文章中发现的解决方案。

<?php

require_once "vendor/autoload.php";

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;                          
//Provide username and password     
$mail->Username = "[email protected]";                 
$mail->Password = "password";                           
//If SMTP requires TLS encryption then set it
//$mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 587;                                   

$mail->From = "[email protected]";
$mail->FromName = "Full Name";

$mail->smtpConnect(
    array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true
        )
    )
);

$mail->addAddress("[email protected]", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

This won't require any server settings on your localhost.

这不需要您的本地主机上的任何服务器设置。

$mail->smtpConnect(
    array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true
        )
    )
); 

This part of code asks smtp not to verify any connection and can send the mail without verifying the sender. Also you need to enable IMAP settings from your mailbox settings.

这部分代码要求smtp不验证任何连接,可以在不验证发件人的情况下发送邮件。您还需要从邮箱设置中启用 IMAP 设置。

Here are the links for reference. https://www.sitepoint.com/sending-emails-php-phpmailer/https://github.com/PHPMailer/PHPMailer/issues/368#issuecomment-75821110

这里有链接供参考。 https://www.sitepoint.com/sending-emails-php-phpmailer/ https://github.com/PHPMailer/PHPMailer/issues/368#issuecomment-75821110

回答by jperezov

You need an SMTP server to send out mail. Assuming you want to use this for testing purposes, try downloading a free SMTP local server such as this one.

您需要一个 SMTP 服务器来发送邮件。假设您想将其用于测试目的,请尝试下载一个免费的 SMTP 本地服务器,例如这个

If you want to actually send out mail in a production environment, consider using an external service such as SendGrid or MailChimp. Alternatively, if you want to stick with SMTP, you aregoing to need your own web server to send mail from.

如果您想在生产环境中实际发送邮件,请考虑使用外部服务,例如 SendGrid 或 MailChimp。另外,如果你想坚持SMTP,您将需要自己的Web服务器来发送邮件时。

回答by adelowo

You shouldn't comment out the line that tells the mailer to use smtp except you really want to use the normal mail function,which i don't think you want to

你不应该注释掉告诉邮件程序使用 smtp 的行,除非你真的想使用普通的邮件功能,我认为你不想

<?php
 require 'PhpMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
    $mail = new PHPMailer;   
    $mail->isSMTP();
// change this to 0 if the site is going live
    $mail->SMTPDebug = 2;
    $mail->Debugoutput = 'html';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';

 //use SMTP authentication
    $mail->SMTPAuth = true;
//Username to use for SMTP authentication
    $mail->Username = "[email protected]";
    $mail->Password = "******";
    $mail->setFrom('[email protected]', 'Somebody');
    $mail->addReplyTo('[email protected]', 'Somebody');
    $mail->addAddress('[email protected]', 'Somebody');
    $mail->Subject = 'New contact from somebody';
    // $message is gotten from the form
    $mail->msgHTML($message);
$mail->AltBody = $filteredmessage;
    if (!$mail->send()) {
        echo "We are extremely sorry to inform you that your message
could not be delivered,please try again.";
    } else {
        echo "Your message was successfully delivered,you would be contacted shortly.";
        }
?> 

please note you must be connected to the internet for gmail's smtp to work

请注意,您必须连接到互联网才能使用 gmail 的 smtp