Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和Sendgrid API发送表单文件附件_Php_Email_Attachment_Phpmailer_Sendgrid - Fatal编程技术网

使用PHP和Sendgrid API发送表单文件附件

使用PHP和Sendgrid API发送表单文件附件,php,email,attachment,phpmailer,sendgrid,Php,Email,Attachment,Phpmailer,Sendgrid,下面是一个使用phpmailer通过php sendmail发送电子邮件的脚本。我想使用Sendgrid的API发送电子邮件,我需要做哪些更改才能使其与Sendgrid一起工作 <?php require('PHPMailer/class.phpmailer.php'); if(isset($_POST['email'])) { $name = $_POST['name']; // required $position = $_POST['position']; //

下面是一个使用phpmailer通过php sendmail发送电子邮件的脚本。我想使用Sendgrid的API发送电子邮件,我需要做哪些更改才能使其与Sendgrid一起工作

<?php

require('PHPMailer/class.phpmailer.php');

if(isset($_POST['email'])) {
    $name = $_POST['name']; // required
    $position = $_POST['position']; // required
    $email_from = $_POST['email']; // required
    $phone = $_POST['phone']; // not required
    $comments = $_POST['comments']; // required

    $email_to = "careers@site.com";
    $name_to = "careers";
    $email_subject = "Job Application from ".$name." for ".$position;

  if($position != null)
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     $email_message = "";
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Position: ".clean_string($position)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Phone: ".clean_string($phone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

    $allowedExts = array("doc", "docx", "xls", "xlsx", "pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/excel")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel")
|| ($_FILES["file"]["type"] == "application/x-excel")
|| ($_FILES["file"]["type"] == "application/x-msexcel")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))

&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "<script>alert('Error: " . $_FILES["file"]["error"] ."')</script>";
    }
  else
    {
        $d='upload/';
        $de=$d . basename($_FILES['file']['name']);
    move_uploaded_file($_FILES["file"]["tmp_name"], $de);
$fileName = $_FILES['file']['name'];
    $filePath = $_FILES['file']['tmp_name'];
     //add only if the file is an upload
     }
  }
else
  {
  echo "<script>alert('Invalid file')</script>";
  }

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Set who the message is to be sent from
$mail->SetFrom($email_from, $name);
//Set an alternative reply-to address
$mail->addReplyTo($email_from, $name);
//Set who the message is to be sent to
$mail->addAddress($email_to, $name_to);
//Set the subject line
$mail->Subject = $email_subject;
//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
$mail->MsgHTML($email_message);
//Replace the plain text body with one created manually
$mail->AltBody = $email_message;
//Attach an image file
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
//Send the message, check for errors
if(!$mail->Send()) {
  echo "<script>alert('Mailer Error: " . $mail->ErrorInfo."')</script>";
} else {
  echo "<script>alert('Your request has been submitted. We will contact you soon.')</script>";
}
}
?>

以下是来自phpmailer的代码,您可以在实例化phpmailer后指定sendgrid smtp参数

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

以下是来自phpmailer的代码,您可以在实例化phpmailer后指定sendgrid smtp参数

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

以下是来自phpmailer的代码,您可以在实例化phpmailer后指定sendgrid smtp参数

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

以下是来自phpmailer的代码,您可以在实例化phpmailer后指定sendgrid smtp参数

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
同样,要将代码更改为使用SendGrid和phpMail发送,您需要在实例化phpMail后更新代码以包含SMTP设置。以下设置将适用于SendGrid:

// This should go somewhere after $mail = new PHPMailer();
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.sendgrid.net';                    // Specify server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your_sendgrid_username';           // SMTP username
$mail->Password = 'your_sendgrid_password';           // SMTP password
$mail->Port = 587;                                    // Recommended Port
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl'
同样,要将代码更改为使用SendGrid和phpMail发送,您需要在实例化phpMail后更新代码以包含SMTP设置。以下设置将适用于SendGrid:

// This should go somewhere after $mail = new PHPMailer();
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.sendgrid.net';                    // Specify server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your_sendgrid_username';           // SMTP username
$mail->Password = 'your_sendgrid_password';           // SMTP password
$mail->Port = 587;                                    // Recommended Port
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl'
同样,要将代码更改为使用SendGrid和phpMail发送,您需要在实例化phpMail后更新代码以包含SMTP设置。以下设置将适用于SendGrid:

// This should go somewhere after $mail = new PHPMailer();
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.sendgrid.net';                    // Specify server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your_sendgrid_username';           // SMTP username
$mail->Password = 'your_sendgrid_password';           // SMTP password
$mail->Port = 587;                                    // Recommended Port
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl'
同样,要将代码更改为使用SendGrid和phpMail发送,您需要在实例化phpMail后更新代码以包含SMTP设置。以下设置将适用于SendGrid:

// This should go somewhere after $mail = new PHPMailer();
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.sendgrid.net';                    // Specify server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your_sendgrid_username';           // SMTP username
$mail->Password = 'your_sendgrid_password';           // SMTP password
$mail->Port = 587;                                    // Recommended Port
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl'


谢谢你的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。如何更改代码以支持此功能?SendGrid对文件的存储位置没有任何偏好。您链接到的代码是我们的PHP客户端库,它确实需要存储文件,但最终,该库只是将数据获取到API的一种方式。您正在使用SMTP作为方法,因此无需担心。上面的代码可以很好地用于发送附件。如果不是这样,我可能建议您使用我们的库而不是
PHPMailer
,您可以使用
addAttachment($\u files['file']['tmp\u name'])附加文件。
。我认为SendGrid自发布以来已更改了要求:-/例如,我认为您需要一个API密钥。这应该仍然适用于SendGrid,SendGrid经常交替使用API密钥/密码。谢谢您的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。如何更改代码以支持此功能?SendGrid对文件的存储位置没有任何偏好。您链接到的代码是我们的PHP客户端库,它确实需要存储文件,但最终,该库只是将数据获取到API的一种方式。您正在使用SMTP作为方法,因此无需担心。上面的代码可以很好地用于发送附件。如果不是这样,我可能建议您使用我们的库而不是
PHPMailer
,您可以使用
addAttachment($\u files['file']['tmp\u name'])附加文件。
。我认为SendGrid自发布以来已更改了要求:-/例如,我认为您需要一个API密钥。这应该仍然适用于SendGrid,SendGrid经常交替使用API密钥/密码。谢谢您的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。如何更改代码以支持此功能?SendGrid对文件的存储位置没有任何偏好。您链接到的代码是我们的PHP客户端库,它确实需要存储文件,但最终,该库只是将数据获取到API的一种方式。您正在使用SMTP作为方法,因此无需担心。上面的代码可以很好地用于发送附件。如果不是这样,我可能建议您使用我们的库而不是
PHPMailer
,您可以使用
addAttachment($\u files['file']['tmp\u name'])附加文件。
。我认为SendGrid自发布以来已更改了要求:-/例如,我认为您需要一个API密钥。这应该仍然适用于SendGrid,SendGrid经常交替使用API密钥/密码。谢谢您的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。如何更改代码以支持此功能?SendGrid对文件的存储位置没有任何偏好。您链接到的代码是我们的PHP客户端库,它确实需要存储文件,但最终,该库只是将数据获取到API的一种方式。您正在使用SMTP作为方法,因此无需担心。上面的代码可以很好地用于发送附件。如果不是这样,我可能建议您使用我们的库而不是
PHPMailer
,您可以使用
addAttachment($\u files['file']['tmp\u name'])附加文件。
。我认为SendGrid自发布以来已更改了要求:-/例如,我认为您需要一个API密钥。这应该仍然适用于SendGrid,SendGrid经常交替使用API密钥/密码。谢谢您的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。github.com/sendgrid/sendgrid php#附件。我如何更改代码来支持它?谢谢你的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。github.com/sendgrid/sendgrid php#附件。我如何更改代码来支持它?谢谢你的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。github.com/sendgrid/sendgrid php#附件。我如何更改代码来支持它?谢谢你的回答。Sendgrid需要磁盘上的文件附件,而不是存储在内存中的文件。github.com/sendgrid/sendgrid php#附件。如何更改代码以支持这一点?