如何使用phpmailer成功发送邮件

时间:2019-07-15 09:55:33

标签: php email phpmailer

我不使用作曲家。

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

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

我已经从多个站点复制了代码,并尝试了至少五个 但没有一个成功。当前代码是我尝试过的最新代码

我尝试或查看了这些链接,但对我没有帮助。 这是我的不得已的方法,因为我也在网站上尝试过教程。

phpmailer can't send mail

phpmailer not send mail

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

Sending mail with PHPMailer (SMTP)

Failed opening required 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php')

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中找不到类'PHPMailer'

PHP警告:require(class.PHPMailer.php):无法打开流:在第2行的/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')在第2行的/home/elcomass/public_html/elcomass-sendmail.php中

我尝试制作文件夹,在其中手动放置phpmailer项目。 现在,它直接放在我的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获得了该网站的电子邮件地址规范。

1 个答案:

答案 0 :(得分:-1)

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

<?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;
        }   
    }
    ?>