Last active
April 1, 2016 23:37
-
-
Save korniychuk/f46503affb89b73e0b35ecdb4627907e to your computer and use it in GitHub Desktop.
Swift mailer simple example
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": { | |
"swiftmailer/swiftmailer": "@stable" | |
} | |
} |
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 ('./sender.php'); | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$to = isset($_POST['to']) ? $_POST['to'] : null; | |
$title = isset($_POST['title']) ? $_POST['title'] : null; | |
$body = isset($_POST['body']) ? $_POST['body'] : null; | |
$result = sendHtmlMail($to, $title, $body); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Test email sending</title> | |
</head> | |
<body> | |
<h1>Тестовая отправка письма</h1> | |
<?php if( !isset($result) ): ?> | |
<form action="" method="post"> | |
<p>Кому слать <input type="text" name="to"></p> | |
<p>Заголовок <input type="text" name="title"></p> | |
<p>Тело(html) <textarea name="body" cols="30" rows="10"></textarea></p> | |
<p><button type="submit">send</button></p> | |
</form> | |
<?php elseif($result) : ?> | |
<strong style="color: green">Успешно отправлено</strong> | |
<a href="form.php">Отправить еще</a> | |
<?php else: ?> | |
<strong style="color: red">Успешно отправлено</strong> | |
<?php endif ?> | |
</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'; | |
// создаем транспорт | |
$transport = Swift_SmtpTransport::newInstance('smtp-pulse.com', 2525) | |
->setUsername('[email protected]') | |
->setPassword('HSDIFUSDF'); // your password | |
$mailer = Swift_Mailer::newInstance($transport); | |
Swift_Preferences::getInstance()->setCacheType('array'); | |
/** | |
* Отправить html письмо | |
* | |
* @param string $to Кому отправлять | |
* @param string $title | |
* @param string $body | |
* | |
* @return boolean Успешно отправлено? | |
*/ | |
function sendHtmlMail($to, $title, $body) { | |
global $mailer; | |
// создаем сообщение | |
$message = Swift_Message::newInstance($title) | |
// sender | |
->setFrom(['[email protected]' => 'My Site robot']) | |
// email will send to this address | |
->setTo($to) | |
// Default text body | |
->setBody($body); | |
// And optionally an alternative body | |
//->addPart('<q>Here is the message itself</q>', 'text/html') | |
// Optionally add any attachments | |
// ->attach(Swift_Attachment::fromPath('/home/file.pdf')); | |
// шлем сообщение | |
$result = $mailer->send($message); | |
return (bool)$result; | |
} |
stavitskiyE
commented
Apr 1, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment