【问题标题】:Sending email over SMTP with sendgrid使用 sendgrid 通过 SMTP 发送电子邮件
【发布时间】:2019-02-11 16:56:18
【问题描述】:

我有一个 html 字段集和一个简短的 php 文件,用于在单击提交按钮时通过 sendgrid 发送电子邮件。

HTML

            <form id="contact-form" action="scripts/mailer.php" method="post">
                <fieldset form="#contact-form">
                    <legend>Contact Form</legend>
                    <label class="input-field-name">Name:<br />
                        <input class="input-field" type="text" name="name" required/>
                    </label><br />
                    <label class="input-field-name">Email:<br />
                        <input class="input-field" type="text" name="email" required/>
                    </label><br />
                    <label class="input-field-name">Message Title:<br />
                        <input class="input-field" type="text" name="header" required/>
                    </label><br />
                    <label class="input-field-name">Message:<br />
                        <textarea class="message-field" type="text" name="message" required></textarea>                            
                    </label><br />
                    <button id="submit-button" type="submit">Submit</button>
                </fieldset>
            </form>

PHP

<?php

require_once('sendgrid-php/sendgrid-php.php');

$name = $_POST["name"];
$email = $_POST["email"];
$header = $_POST["header"];
$message = $_POST["message"];

$from = new SendGrid\Email(null, $email);
$subject = $header;
$to = new SendGrid\Email(null, "MY EMAIL ADDRESS");
$content = new SendGrid\Content("text/plain", $message);
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = 'MY API KEY';
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
if($response->statusCode() == 202){
    echo "Email sent successfully";
}else{
    echo "Email could not be sent";
}

echo $response->statusCode();
var_dump($response->headers());
echo $response->body(); 

我在 GoDaddy 上托管我的网站,根据我一直在阅读的所有内容,我相信这应该可以工作,但我收到 http 错误 500,我的网站无法处理此请求。

关于如何解决这个问题的任何想法?谢谢

【问题讨论】:

  • 当然,这是一个伪造的apiKey .... 对吗???如果不知道该怎么做。
  • 感谢上帝
  • 列出来自日志文件服务器 500 的错误 - 致命错误 php
  • 我不知道 Sendgrid 的 api,但您的代码似乎很奇怪。你为什么不试着捕捉整个事情,捕捉异常和可抛出,看看有什么给出(假设你没有合适的 IDE)。一般来说,您应该进行防御性编码,并使用大量的故障检测/日志记录等……因为这样,您的飞行没有安全网。\
  • @PumpkinBreath 您的 API 密钥在编辑历史记录中并且仍然是公开的。您也可以开始撤销它的过程,因为它已受到损害(或者您知道您可以忍受它受到损害)。如果我戴一顶黑帽子,我也会为那些看起来像历史上这样的钥匙的东西刮掉。

标签: php html email sendgrid


【解决方案1】:

我管理的服务器之一是 GoDaddy,当使用 SendGrid 的 SMTP 服务发送电子邮件时,设置以下附加字段是关键:

$username = 'apikey';
$host = 'ssl://smtp.sendgrid.net';
$port = 587;

设置用户名时,请使用如上所示的字符串“apikey”,而不是您的实际 apikey。 GoDaddy 在使用端口 587 时似乎运行良好,将主机设置为上述端口时,将安全地发送您的电子邮件。

您的电子邮件应该在此之后工作。祝你好运。

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2011-04-06
    • 2017-05-12
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多