如何使用phpmailer从本地主机发送电子邮件? [英] How to send e-mail from localhost with phpmailer?

查看:130
本文介绍了如何使用phpmailer从本地主机发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的mail.php

here is my 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?

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?

推荐答案

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

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 = "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";
}

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

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
        )
    )
); 

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

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.

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

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

这篇关于如何使用phpmailer从本地主机发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆