简体   繁体   English

Mailgun与PHP

[英]Mailgun with PHP

I've been trying to implement a contact service using Mailgun coded in PHP. 我一直在尝试使用PHP编码的Mailgun实施联系服务。 I receive the following error: 我收到以下错误:

Could not connect to SMTP host 无法连接到SMTP主机

Below is my code: 下面是我的代码:

<?php

require("../includes/config.php");
require("../mailgun-php/vendor/autoload.php");
require("../phpmailer/_lib/class.phpmailer.php");
use Mailgun\Mailgun;

if($_SERVER["REQUEST_METHOD"] === "GET")
{
    render("contact-form.php", ["title" => "Contact us"]);
}

if($_SERVER["REQUEST_METHOD"] === "POST")
{     

    $mail = new PHPMailer;

    $mail->isSMTP();  // Set mailer to use SMTP
    $mail->Host = 'smtp.mailgun.org';  // Specify mailgun SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = 'postmaster@sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // SMTP username from https://mailgun.com/cp/domains
    $mail->Password = '*SMTP password from sandbox domain'; // SMTP password from https://mailgun.com/cp/domains
    $mail->SMTPSecure = 'sll';   // Enable encryption, 'ssl'
            $mail->Port= '465';

    $mail->From = 'sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // The FROM field, the address sending the email 
    $mail->FromName = 'Enquiry bot'; // The NAME field which will be displayed on arrival by the email client
    $mail->addAddress('****@gmail.com');     // Recipient's email address and optionally a name to identify him
    $mail->isHTML(true);   // Set email to be sent as HTML, if you are planning on sending plain text email just set it to false

    // The following is self explanatory
    $mail->Subject = 'Client enquiry';
    $mail->Body    = $_POST["message"];

    if(!$mail->send())
    {  
        echo "Message hasn't been sent.";
        echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
    } 
    else 
    {
        redirect("confirmation.html");
    }
}
?>

Note that this is the controller (I'm using MCV). 请注意,这是控制器(我正在使用MCV)。
This is part of a website I've been implementing as a project for a CS class. 这是我作为CS类项目实施的网站的一部分。 Therefore I would avoid buying a private domain. 因此,我将避免购买私有域。

UPDATE : I've modified the original post with how this piece of code should be. 更新 :我已经用这段代码修改了原始帖子。 May anyone else encounter this issue, this snippet is working without any issue. 可能其他任何人都遇到此问题,此代码段可以正常工作。

I've contacted the Mailgun staff which provided me with excellent support. 我已经联系Mailgun员工,这为我提供了出色的支持。 The problem lies with how I specify the port. 问题在于我如何指定端口。 It should be 465 for SSL and 587 for TLS. 对于SSL,应为465,对于TLS,应为587。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM