如何在共享托管环境中安装phpMailer?

时间:2018-03-07 22:43:40

标签: phpmailer

如何在共享托管环境中安装phpMailer?

看到这个How to install phpMailer in a shared hosting environment?,但我不明白"包含主要文件的内容:"意味着和下一部分也是#34;之后,你将需要一个外部SMTP帐户,例如gmail.com。以下是使用GMAIL的PHP​​Mailer的一个工作示例:"

提前致谢 Athlios

这是send_form.php

<?php

$formid = $_POST['contactform'];

$email_to = "info@a-wd.eu";

$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
//$subjectselect = $_POST('subject').value();
$message = $_POST['message']; // required

echo($email_from);

$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message,$headers) or die("Error!");

echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
?>

这是页面

<div class="container-fluid">
      <div class="row">
            <div class="col-12">
        <div class="container contact-form-container">
      <div class="row">
        <div class="col-md-12">
          <form action="send_form.php" id="contactform" name="contactform" method="post" data-parsley-validate>
            <div class="row">
            <!-- <span class="required-key">Fields marked with a <span class="label-required">*</span> are required.</span> -->
            <div class="form-group col-lg-12 col-12">
              <label for="fullname">Name <span>(Required)</span>:</label>
              <input type="text" name="fullname" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
            </div>
            <div class="form-group col-lg-12 col-12">
              <label for="email">Email <span>(Required)</span>:</label>
              <input type="email" data-parsley-type="email" name="email" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
            </div>
            <div class="form-group col-lg-12 col-12">
              <label for="subject">Subject <span>(Required)</span>:</label>
              <select name="subject" required data-parsley-required="true" data-parsley-errors-messages-disabled>
                <option value="question">General Question</option>
                <option value="quote">Request a Quote</option>
                <option value="sponsorship">Sponsorship</option>
                <option value="other">Other</option>
              </select>
            </div>
            <div class="form-group col-lg-12 col-12">
              <label for="message">Message <span>(Required)</span>:</label>
              <textarea name="message" data-parsley-trigger="focusin focusout" data-parsley-minlength="20" data-parsley-maxlength="1000" data-parsley-validation-threshold="10" data-parsley-minlength-message="Minimum message length is 20 characters" data-parsley-maxlength-message="Maximum message length is 1000 characters" data-parsley-required="true"></textarea>
            </div>
            <div class="form-group col-lg-12 col-12">
                <label for="message">Captcha <span>(Required)</span>:</label>
                <script src='https://www.google.com/recaptcha/api.js'></script>
                <div class="g-recaptcha" data-sitekey="6LeAAUkUAAAAAJeW7fjroLKFkYtETHvXGgflK49u"></div>
            </div>
            <div class="form-group col-lg-12 col-12">
              <button class="btn-contact" name="send" type="submit">Send Message <i class="fa fa-paper-plane" aria-hidden="true"></i></button>
            </div>
          </form>
          </div>
        </div>
      </div>
        </div>
             </div>
          </div>
        </div>

1 个答案:

答案 0 :(得分:1)

转到PHPMailer github,点击绿色&#34;克隆或下载&#34;按钮,然后单击&#34;下载ZIP&#34;。在本地计算机上,取消归档ZIP文件并将PHPMailer-master文件夹上传到共享服务器的public_html目录。

现在,无论您想在何处使用PHPMailer,请包含您需要的部分:

<?php
require '/path/to/PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require '/path/to/PHPMailer-master/src/Exception.php'; // If you want to debug
?>

如PHPMailer文档中所述,您不需要外部SMTP帐户才能使用此帐户。因此,在您发送电子邮件的脚本中,您的代码应如下所示:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug


$mail = new PHPMailer(true);                              // Passing `true` enables exceptions

try {

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

这应该是您在共享服务器上开始使用PHPMailer所需的全部内容。查看项目的README并查看一些examples,以便更好地了解您使用此库获得的所有精彩内容。

更新OP的代码

将包含代码放在send_post.php

的顶部
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug

// Form details
$formid = $_POST['contactform'];

$email_to = "info@a-wd.eu";

$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required

$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");

// No need to set headers here

// Replace the mail() function with PHPMailer

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions

try {

    //Recipients
    $mail->setFrom($email_from, 'From Name');
    $mail->addAddress($email_to, $fullname);     // Add the recipient

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

    $mail->send();
    echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}