duanmeng3573 2014-03-24 22:08
浏览 63
已采纳

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

Below is a script used too send email through php sendmail using phpmailer. I want to send an email using Sendgrid's API, what changes do I need to make in order to make it work with 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)."
";
    $email_message .= "Position: ".clean_string($position)."
";
    $email_message .= "Email: ".clean_string($email_from)."
";
    $email_message .= "Phone: ".clean_string($phone)."
";
    $email_message .= "Comments: ".clean_string($comments)."
";

    $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."
".
'Reply-To: '.$email_from."
" .
'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>";
}
}
?>
  • 写回答

2条回答 默认 最新

  • dor65412 2014-03-24 22:50
    关注

    As hackerone said, to change your code to send with SendGrid and PHPMailer, you'll need to update your code to include SMTP settings after instantiating PHPMailer. The following settings will work with 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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。