email - Send form file attachment using PHP and Sendgrid APIs -
below script used send email through php sendmail using phpmailer. want send email using sendgrid's api, changes need make in order make work 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 ".$name." ".$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 if file 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 new phpmailer instance $mail = new phpmailer(); //set message sent $mail->setfrom($email_from, $name); //set alternative reply-to address $mail->addreplyto($email_from, $name); //set message sent $mail->addaddress($email_to, $name_to); //set subject line $mail->subject = $email_subject; //read html message body external file, convert referenced images embedded, convert html basic plain-text alternative body $mail->msghtml($email_message); //replace plain text body 1 created manually $mail->altbody = $email_message; //attach image file $mail->addattachment($_files['file']['tmp_name'], $_files['file']['name']); //send message, check errors if(!$mail->send()) { echo "<script>alert('mailer error: " . $mail->errorinfo."')</script>"; } else { echo "<script>alert('your request has been submitted. contact soon.')</script>"; } } ?>
as hackerone said, change code send sendgrid , phpmailer, you'll need update code include smtp settings after instantiating phpmailer. following settings work sendgrid:
// should go somewhere after $mail = new phpmailer(); $mail->issmtp(); // set mailer 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'
Comments
Post a Comment