下面是使用phpmailer通過php sendmail發送電子郵件的腳本。我想使用Sendgrid的API發送電子郵件,爲了使它與Sendgrid一起工作,我需要做些什麼改變?使用PHP和Sendgrid API發送表單文件附件
<?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 = "[email protected]";
$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>";
}
}
?>
感謝您的回答。 Sendgrid需要磁盤附件上的文件,而不是將文件存儲在內存中。 https://github.com/sendgrid/sendgrid-php#attachments。我如何更改我的代碼來支持它? – Steve
SendGrid對您存儲文件的位置沒有任何偏好。您鏈接到的代碼是我們的PHP客戶端庫,它確實需要存儲的文件,但最終,庫只是一種將數據傳輸到我們的API的方式。您使用SMTP作爲您的方法,因此不需要擔心。上面的代碼應該可以很好地發送附件。如果不是,我可能會推薦使用我們的庫而不是'PHPMailer',你可以使用'addAttachment($ _ FILES ['file'] ['tmp_name'])''附加文件。 –
謝謝。很棒! – Steve