如何使用phpmailer从localhost发送电子邮件?

时间:2015-02-15 02:47:32

标签: php html email localhost

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

这是我的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 = "henrikus.antony@gmail.com"; // 
$mail->Password = "******"; // SMTP password
$webmaster_email = "henrikus.antony@gmail.com"; //Reply to this email ID
$email = "rikunime.share@gmail.com"; // 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";
}
?>
请某人,我真的需要帮助。我需要托管吗?或者我的语法有问题? sendmail.ini和php.ini是否会影响mail.php?

3 个答案:

答案 0 :(得分:6)

这是我从几篇文章中找到的解决方案。

<?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 = "name@gmail.com";                 
$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 = "name@gmail.com";
$mail->FromName = "Full Name";

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

$mail->addAddress("reciever@ymail.com", "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";
}

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

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

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

以下是供参考的链接。 https://www.sitepoint.com/sending-emails-php-phpmailer/ https://github.com/PHPMailer/PHPMailer/issues/368#issuecomment-75821110

答案 1 :(得分:2)

您需要SMTP服务器才能发送邮件。假设您要将其用于测试目的,请尝试下载免费的SMTP本地服务器,例如this one

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

答案 2 :(得分:1)

你不应该注释掉告诉邮件人使用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 = "xxxxxx@gmail.com";
    $mail->Password = "******";
    $mail->setFrom('xxx@ww.com', 'Somebody');
    $mail->addReplyTo('xxx@ww.com', 'Somebody');
    $mail->addAddress('xxx@ww.com', '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.";
        }
?> 

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