如何使用phpmailer成功发送邮件

How to successfully send mail with phpmailer

我不使用作曲家。

我有一个简单的联系表,我想通过它发送文本 我网站的电子邮件帐户到 gmail。

http://elcomass.com/elcomass-contact.php

我从多个站点复制了代码并尝试了至少五个 但是 none 有效。当前代码是我尝试过的最新代码

我尝试或查看了这些链接,但没有帮助。 这是我最后的选择,因为我也试过网站上的教程。

phpmailer can't send mail

phpmailer not send mail

https://github.com/PHPMailer/PHPMailer#a-simple-example

Sending mail with PHPMailer (SMTP)

https://codeforgeek.com/phpmailer-ultimate-tutorial/

https://www.cloudways.com/blog/send-emails-in-php-using-phpmailer/

https://alexwebdevelop.com/phpmailer-tutorial/

https://www.inmotionhosting.com/support/email/send-email-from-a-page/using-phpmailer-to-send-mail-through-php

目前正在研究: https://www.google.com/search?client=firefox-b-d&q=PHP+Fatal+error%3A++require%28%29%3A+Failed+opening+required+%27class.PHPMailer.php%27

当您提交表单时,您得到的只是一个空白页面。 在错误日志中,这是最常见的问题:

PHP 致命错误:在第 6

行的 /home/elcomass/public_html/elcomass-sendmail.php 中找不到 Class 'PHPMailer'

PHP 警告:要求(class.PHPMailer.php):无法打开流:第 2[=22= 行的 /home/elcomass/public_html/elcomass-sendmail.php 中没有此类文件或目录]

[2019 年 7 月 13 日 12:27:14 UTC] PHP 致命错误:require():需要打开失败 'class.PHPMailer.php' (include_path='.:/opt/cpanel/ea-php56/root/usr/share/pear') 在 /home/elcomass/public_html/elcomass-sendmail.php 第 2

我试过制作文件夹,手动将 php 邮件项目放在那里。 就像现在一样,没有文件夹,直接放在我的public_html 它仍然不起作用。 Exception.php、STMP.php

相同
<?php
// This is one way I tried
use PHPMailer;
require 'PHPMailer.php';
// #2
require 'class.PHPMailer.php';
// #3
require 'PHPMailer.php';
// #4
require 'test/class.PHPMailer.php';
// #5
require 'test\class.PHPMailer.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$subject = $_POST['subject'];
$message = $_POST['message'];
$email = $_POST['email'];
$name = $_POST['name'];

try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'xxxx';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'xxxxxx@xxxxxx.com';                     // SMTP username
    $mail->Password   = 'xxxx';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = xxx;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom($email, $name);
    $mail->addAddress('xxx@gmail.com', 'elcomass');     // Add a recipient



    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = $message;

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

我想要联系表将电子邮件发送到 gmail 地址。 我通过 cPanel 获得了网站的电子邮件地址规范。

使用此代码。它对我有用。

<?php
    require_once('phpmailer/class.phpmailer.php');

    function send_email($mail_information=array())
    {
        $receiver_email = $mail_information['receiver_email']; 
        $receiver_name = $mail_information['receiver_name'];
        $reply_to_email = '***@gmail.com';
        $reply_to_name = $mail_information['reply_to_name'];
        $message = $mail_information['message'];
        $subject = $mail_information['subject'];

        $from_email = $reply_to_email; 
        $from_name = "TEST";  
        $mail             = new PHPMailer();
        $body             = $message;
        $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                                   // 1 = errors and messages
                                                   // 2 = messages only
        $mail->SMTPAuth   = false;                  // enable SMTP authentication
        $mail->Host       = "localhost"; // sets the SMTP server
        $mail->Username   = "***@test.com"; // SMTP account username
        $mail->Password   = "*****";        // SMTP account password
        $mail->Port = 25; // or 587
        $mail->SMTPSecure = 'No'; // secure transfer enabled REQUIRED for GMail
        $mail->IsSMTP(); 
        $mail->IsHTML(true);
        $mail->SetFrom($from_email, $from_name);

        if($reply_to_email != '' && $reply_to_name != '')
        {
            $mail->AddReplyTo($reply_to_email, $reply_to_name);
        }

        $mail->Subject    = $subject;

        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer !"; // optional, comment out and test

        $mail->MsgHTML($body);

        $address = $receiver_email;
        $mail->AddAddress($address, $receiver_email);


        if(! $mail->Send()) 
        {
            echo "<br>Mailer Error: " . $mail->ErrorInfo;
            return false;
        } 
        else 
        {

            return true;
        }   
    }
    ?>

经过大量使用 phpmailer,我设法解决了我的问题。如错误所示,我做错了几件事,但对于任何感兴趣的人,请小心复制有关您的域电子邮件的详细信息,并且不要在 phpmailer 代码所在的同一页面中调用 SESSION。