Last active
June 10, 2024 14:46
-
-
Save daveh/1da54bbd6e993d29b4002e7f029173c5 to your computer and use it in GitHub Desktop.
Send an email with an attachment using an API in PHP (code to accompany https://youtu.be/6ncbK1aBl1w)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"wildbit/postmark-php": "^5.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Send email Example</title> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" /> | |
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script> | |
<script> | |
tinymce.init({ | |
selector: "#body", | |
menubar: false, | |
toolbar: "bold italic", | |
statusbar: false, | |
height: 160 | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Send email Example</h1> | |
<form method="post" enctype="multipart/form-data" action="process-form.php"> | |
<label for="name">Name</label> | |
<input type="text" name="name" id="name" required /> | |
<label for="email">email</label> | |
<input type="email" name="email" id="email" required /> | |
<label for="subject">Subject</label> | |
<input type="text" name="subject" id="subject" required /> | |
<label for="body">Message</label> | |
<textarea id="body" name="body" required></textarea> | |
<label for="file">Attachment</label> | |
<input type="file" id="file" name="file" /> | |
<br /> | |
<button>Send</button> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . "/vendor/autoload.php"; | |
use Postmark\PostmarkClient; | |
use Postmark\Models\PostmarkAttachment; | |
use Postmark\Models\PostmarkException; | |
$name = $_POST["name"]; | |
$email = $_POST["email"]; | |
$subject = $_POST["subject"]; | |
$body = $_POST["body"]; | |
$file_path = $_FILES["file"]["tmp_name"]; | |
$file_name = $_FILES["file"]["name"]; | |
$file_type = mime_content_type($file_path); | |
if ( ! is_uploaded_file($file_path)) { | |
throw new UnexpectedValueException("Invalid file"); | |
} | |
try { | |
$client = new PostmarkClient("postmark API key here"); | |
$attachment = PostmarkAttachment::fromFile($file_path, | |
$file_name, | |
$file_type); | |
$result = $client->sendEmail( | |
from: "Name <[email protected]>", | |
to: "$name <$email>", | |
subject: $subject, | |
htmlBody: $body, | |
textBody: strip_tags($body), | |
attachments: [$attachment] | |
); | |
echo "Message sent."; | |
} catch (PostmarkException $e) { | |
echo $e->message; | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tag
<?php /// do something ?>