apache_conf 使用PHPMailer在PHP中发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache_conf 使用PHPMailer在PHP中发送电子邮件相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Start the timer
 */
$start_time = microtime(true);

/*Autoload file from phpmailer library
you can download this library from composer*/
require "../vendor/phpmailer/phpmailer/PHPMailerAutoload.php";
require "../classes/Config.php";

//Enable errors to display
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//creating object for PHPMailer class in library
$mail = new PHPMailer();
try {
//configuration to send email
$mail->isSMTP();
$mail->Host = Config::SMTP_HOST;
$mail->Port = Config::SMTP_PORT;
$mail->SMTPAuth = true;
$mail->Username = Config::SMTP_USER;
$mail->Password = Config::SMTP_PASSWORD;
$mail->SMTPSecure = 'tls';
$mail->CharSet = 'UTF-8';

//Allow to display more debugging information from phpmailer
//$mail->SMTPDebug = 2;
    
//Send an email
$mail->setFrom('phpmailmail@gmail.com', 'gopibabu');
$mail->addAddress('gopinyca.php@gmail.com');
$mail->addAddress('s.gopibabu@gmail.com');
$mail->addCC('gopinyca.php@gmail.com');
$mail->addBCC('gopinyca.php@gmail.com');
//Add different reply address

$mail->addReplyTo('s.gopibabu@gmail.com','gopibabu');
    
//Adding Attachments and these attachements are in same directory where script resides.
//__FILE__ gives the full path of the current file_exists
//dirName() give the path of the directory where current file residing, without including file name.
$mail->addAttachment(dirName(__FILE__).'/sampleDoc.txt', 'new.txt');

//Allow HTML in Content
$mail->isHTML(true); 
$mail->Subject  = 'Testing PHPMailer Messages with styles and new Imag';
$mail->Body = 'Hi this is testing for sending email from <b style="color: red; font-style: italic">
php mail service.</b> 
and it is send by <b>Gopi</b>'.'<br>'.'<img src="cid:newImage">';

//This is alternative message that we need to send for non-html supported clients.
$mail->AltBody = "Hello/ \n this is just alternative text for non html supported email clients";

//Displaying Inline image in the body of the message.
$mail->addEmbeddedImage(dirName(__FILE__).'/sampleImage.jpg', 'newImage');

//This Action will send the message that we build
$mail->send();
}
catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

$end_time = microtime(true);
$time = number_format($end_time - $start_time, 5);

/**
 * Return to index.php
 */
header("Location:index.php?time=$time");
exit();
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Send an email</title>
    <style>
.hidden{
    display: none;
}
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#emailForm').on('submit', function () {
                $('#sendButton').prop('disabled', true);
                //Download image from http://ajaxload.info/
                $('#progressImage').show();
            });
        });
    </script>
</head>
<body>

<h1>Send an email</h1>

<p><?php echo isset($_GET['time']) ? $_GET['time'] : ''; ?></p>

<form action="send.php" method="post" id="emailForm">
    <button type="submit" id="sendButton">Send</button>
    <!-- Download Image from http://ajaxload.info/ -->
    <img src="loading.gif" width="16" height="16" id="progressImage" class="hidden" />
</form>

</body>
</html>
{
    "require":{
        "phpmailer/phpmailer":"5.*"
    }
}
<?php

/**
 * Here we are saving configuration details related to mail server that we are using to send email
 *
 * Class Config
 */
class Config
{
    /**
     * @var string
     */
    const SMTP_HOST = 'smtp.gmail.com';

    /**
     * @var string
     */
    const SMTP_PORT = 587;

    /**
     * @var string
     */
    const SMTP_USER = 'phpmailmail@gmail.com';

    /**
     * Please make sure that your email not having 2 factor authentication
     * If you have 2 factor authentication, you should enter custom app specific password
     * you can access that from:
     * https://security.google.com/settings/security/apppasswords
     * @var string
     */
    const SMTP_PASSWORD = 'Iamgopibabu';
}

以上是关于apache_conf 使用PHPMailer在PHP中发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

关于 PHPMailer 邮件发送类的使用心得(含多文件上传)

PHPMailer 发送电子邮件并返回带有回显的空白页面

apache_conf Páginasdeerror personalizadas

警告:require_once(./mailer/class.phpmailer.php):打开流失败:

使用 DKIM 密钥在 phpmailer 中发送邮件

使用PHPMailer发送邮件