无法接收来自Azure托管的PHP网站的电子邮件

时间:2017-02-09 15:41:02

标签: php azure sendgrid

我已将网站从简单托管移至Azure云,但现在我的电子邮件尚未发送。我已经安装了SendGrid并保存了用户名和密码。然后我编辑了我的contact.php文件。联系代码.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);
?>

我保存并通过我的浏览器访问它但没有显示任何内容。我查看了我的电子邮件收件箱(垃圾邮件),但没有任何内容。

3 个答案:

答案 0 :(得分:0)

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

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

我希望这可以帮到你。

答案 1 :(得分:0)

你必须在$ 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',
   );

答案 2 :(得分:0)

感谢您的回复,但我通过使用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';
}