PHPMailer只会从localhost发送电子邮件,并且不会从服务器发送

时间:2017-02-01 15:29:56

标签: php email phpmailer html-email attachment

我编写了一个基本脚本,使用PHPMailer发送电子邮件。

当我在本地运行我的脚本以将电子邮件发送给自己时它工作正常但是当我尝试从服务器运行它时,我没有收到电子邮件。我想发送电子邮件而不在PHPMailer中指定SMTP。

PHPMailer库位于codebase目录中,该目录与下面的代码位于同一目录中。在服务器上也一样。

这是我的PHP代码:

$to     = 'aaqib.farooq@example.co.uk';
$toName = 'Aaqib Farooq'

require_once('codebase/PHPMailer/class.phpmailer.php');

$mail             = new PHPMailer;
$mail->From       = "fromname@example.co.uk";
$mail->FromName   = "Joe Bloggs";
$mail->addAddress($to, $toName);
$mail->isHTML(true);
$mail->Subject    = "Subject Text";
$mail->Body       = "<p>Lorem ipsum blah blah blah. This is my messsage.</p>";
$mail->AltBody    = "This is the plain text version of the email content";

if ( !$mail->send() ){
    echo 'Email not sent';
} else {
    echo 'Email was sent successfully';
}

我已经检查了stackoverflow的答案,但无法找到我需要的东西。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

添加此文件:

$mail->IsSMTP();
$mail->Host = 'host of mail';
$mail->Port = 465 or 587; 
$mail->SMTPAuth = true;
$mail->Username = 'username - usually some@mail';
$mail->Password = 'password';

可能您还需要这样的设置:

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

答案 1 :(得分:0)

哎呀。从昨天开始,我一直在努力解决这个问题。事实证明它一直在努力但是我的垃圾。