【问题标题】:Unable to receive emails from PHP website hosted on Azure无法接收来自 Azure 上托管的 PHP 网站的电子邮件
【发布时间】:2017-02-09 15:41:02
【问题描述】:

我已将网站从简单托管移至 Azure Cloud,但现在我的电子邮件无法发送。我已经安装了 SendGrid 并保存了用户名和密码。然后我编辑了我的contact.php 文件。 contact.php的代码是:

 <?php
 $url = 'https://api.sendgrid.com/';
 $user = 'username that I saved';
 $pass = 'password that I created';

 $params = array(
      'api_user' => $user,
      'api_key' => $pass,
      'to' => 'mymailaddress@mail.com',
      'subject' => 'testing from curl',
      'html' => 'testing body',
      'text' => 'testing body',
      'from' => 'anna@contoso.com',
   );

 $request = $url.'api/mail.send.json';

 // Generate curl request
 $session = curl_init($request);

 // Tell curl to use HTTP POST
 curl_setopt ($session, CURLOPT_POST, true);

 // Tell curl that this is the body of the POST
 curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

 // Tell curl not to return headers, but do return the response
 curl_setopt($session, CURLOPT_HEADER, false);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

 // obtain response
 $response = curl_exec($session);
 curl_close($session);

 // print everything out
 print_r($response);
?>

我保存了它并通过我的浏览器访问它,但没有任何显示。我检查了我的电子邮件收件箱(还有垃圾邮件),但那里什么也没有。

【问题讨论】:

    标签: php azure sendgrid


    【解决方案1】:

    你可以在这里找到文档...

    https://docs.microsoft.com/en-us/azure/store-sendgrid-php-how-to-send-email

    希望对你有帮助。

    【讨论】:

    • 我已经阅读了该博客,但它对我不起作用。我找到了另一个解决方案。无论如何,非常感谢@Larry Lurex。
    • 你能发布 print_r($response); ?
    • 就是这个问题,我没有得到任何回应。
    【解决方案2】:

    你必须在 $params array() 中使用x-smtpapi,创建一个带有 to 和 category 元素的 JSON 数组。

    $json_string = array(
          "to" => array(
        "mymailaddress@mail.com"
          ),
          "category" => "subscription"
        );
    

    在 $param array() 中添加这个。

    $params = array(
          'api_user' => $user,
          'api_key' => $pass,
          'x-smtpapi' => json_encode($json_string),
          'to' => 'mymailaddress@mail.com',
          'subject' => 'testing from curl',
          'html' => 'testing body',
          'text' => 'testing body',
          'from' => 'anna@contoso.com',
       );
    

    【讨论】:

    • 谢谢,但另一种解决方案对我有用。非常感谢@Mohd Asim
    【解决方案3】:

    感谢您的回复,但我使用 PHPMailer 解决了这个问题。我下载并上传了它。在那个文件夹中,我用这段代码创建了一个文件,它就像一个 chram。

    require 'PHPMailerAutoload.php'; 
    $mail = new PHPMailer;
    
    //$mail->SMTPDebug = 3;                          // Enable verbose debug output
    
    $mail->isSMTP();                                        // Set mailer to use SMTP
    $mail->Host = 'smtp.sendgrid.net';             // Specify main/backup SMTP servers
    $mail->SMTPAuth = true;                           // Enable SMTP authentication
    $mail->Username = 'username';    // SMTP username
    $mail->Password = 'password';    // SMTP password
    $mail->SMTPSecure = 'tls';                        // Enable TLS/SSL encryption
    
    
    
    $mail->From = "Email of Person";
    $mail->FromName = "Name of Person";
    $mail->addAddress('myaddress@mail.com', 'Support');     // Add a recipient
    
    $mail->WordWrap = 50;                              // Set word wrap to 50 characters
    $mail->isHTML(true);                                  // Set email format to HTML
    
    $mail->Subject = 'Subject is XYZ';
    $mail->Body    =  "A message was sent to you from your website.<br> Email:".$from."<br>Name: ".$name."<br>Message: ".$message;
    
    if(!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
    echo 'Message sent successfully';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多