【问题标题】:Sending Email in PHP and Localhost [duplicate]在 PHP 和 Localhost 中发送电子邮件 [重复]
【发布时间】:2018-11-27 13:09:21
【问题描述】:

我已经搜索了几个小时来寻找解决方案,但我就是不明白。 我对 PHP 还很陌生,我正在做一个需要发送电子邮件的项目,问题是我不知道该怎么做。 其他有同样问题的人,通过配置 php.ini 和 sendemail.ini 来修复它

这是我当前的测试代码:

<?php

$subject="Hi There!!";
$to="mygmail@gmail.com";
$body="This is a demo email sent using PHP on XAMPP";
if (mail($to,$subject,$body)){
 echo "Mail sent successfully!";
}
else{
 echo "Mail not sent!";
}

?>

我收到“邮件发送成功”,但我的收件箱中没有任何内容,甚至垃圾邮件文件夹中也没有。 我也尝试使用 PHPMailer,但(就像我说的)我对 php 相当陌生,我不知道如何在我的代码上启用它。 大家觉得呢?

【问题讨论】:

    标签: php email xampp localhost


    【解决方案1】:
    require 'PHPMailer/PHPMailer.php';
    require 'PHPMailer/Exception.php';
    require 'PHPMailer/OAuth.php';
    require 'PHPMailer/POP3.php';
    require 'PHPMailer/SMTP.php';
    
    $mail = new PHPMailer;
    
    // SMTP configuration
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'user@example.com';
    $mail->Password = '******';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    
    $mail->setFrom('info@example.com', 'CodexWorld');
    $mail->addReplyTo('info@example.com', 'CodexWorld');
    
    // Add a recipient
    $mail->addAddress('john@gmail.com');
    
    // Add cc or bcc 
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');
    
    // Email subject
    $mail->Subject = 'Send Email via SMTP using PHPMailer';
    
    // Set email format to HTML
    $mail->isHTML(true);
    
    // Email body content
    $mailContent = "<h1>Send HTML Email using SMTP in PHP</h1>
        <p>This is a test email has sent using SMTP mail server with PHPMailer.</p>";
    $mail->Body = $mailContent;
    
    // Send email
    if(!$mail->send()){
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }else{
        echo 'Message has been sent';
    }
    

    php 邮件程序库 https://github.com/PHPMailer/PHPMailer

    点击以下链接了解 php mailer https://www.codexworld.com/send-html-email-php-gmail-smtp-phpmailer/

    目录结构

    -root (your proj root directory from where your php file runs)
      -index.php (file with above code)
      -PHPMailer(folder contain mailer library)
         -php mailer files (go to git repo, download and copy all files from src folder)
    

    【讨论】:

    • 嗨 Rupesh,首先我很抱歉给我的帖子投了反对票的两个人(对不起,我不是像你这样的编程天才),非常感谢你的例子,但就像我一样说我真的不明白我应该如何安装PHPMailer,对不起我是个笨蛋。
    • 好的,我已将帖子更新为教程(忽略这些负面标记,我今天也因复杂问题获得了 -ve 分,而且我还从堆栈中获得了帮助。)
    • 好的,非常感谢 Rupesh,现在它就像一个魅力。再次非常感谢你!!
    • 干杯...享受编码
    猜你喜欢
    • 1970-01-01
    • 2016-05-13
    • 2017-06-19
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多