-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.php
79 lines (64 loc) · 2.31 KB
/
email.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
// Load Composer's autoloader
require_once './vendor/autoload.php';
// Import Classess
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
// Create a Transport object
$transport = Transport::fromDsn('smtp://USERNAME:PASSWORD@SERVER:PORT');
// Create a Mailer object
$mailer = new Mailer($transport);
// Create an Email object
$email = (new Email());
// Set the "From address"
$email->from('[email protected]');
// Set the "To address"
$email->to(
# '[email protected]',
);
// Set "CC"
# $email->cc('[email protected]');
// Set "BCC"
# $email->bcc('[email protected]');
// Set "Reply To"
# $email->replyTo('[email protected]');
// Set "Priority"
# $email->priority(Email::PRIORITY_HIGH);
// Set a "subject"
$email->subject('A Cool Subject!');
// Set the plain-text "Body"
$email->text('The plain text version of the message.');
// Set HTML "Body"
$email->html('
<h1 style="color: #fff300; background-color: #0073ff; width: 500px; padding: 16px 0; text-align: center; border-radius: 50px;">
The HTML version of the message.
</h1>
<img src="cid:Image_Name_1" style="width: 600px; border-radius: 50px">
<br>
<img src="cid:Image_Name_2" style="width: 600px; border-radius: 50px">
<h1 style="color: #ff0000; background-color: #5bff9c; width: 500px; padding: 16px 0; text-align: center; border-radius: 50px;">
The End!
</h1>
');
// Add an "Attachment"
$email->attachFromPath('example_1.txt');
$email->attachFromPath('example_2.txt');
// Add an "Image"
$email->embed(fopen('image_1.png', 'r'), 'Image_Name_1');
$email->embed(fopen('image_2.jpg', 'r'), 'Image_Name_2');
// Sending email with status
try {
// Send email
$mailer->send($email);
// Display custom successful message
die('<style> * { font-size: 100px; color: #444; background-color: #4eff73; } </style><pre><h1>🎉Email sent successfully!</h1></pre>');
} catch (TransportExceptionInterface $e) {
// Display custom error message
die('<style>* { font-size: 100px; color: #fff; background-color: #ff4e4e; }</style><pre><h1>😠Error!</h1></pre>');
// Display real errors
# echo '<pre style="color: red;">', print_r($e, TRUE), '</pre>';
}