forked from nishantml/php-mailer-with-custom-html-template-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailer.php
More file actions
93 lines (79 loc) · 3.81 KB
/
mailer.php
File metadata and controls
93 lines (79 loc) · 3.81 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
//display error like in localhost
/* error_reporting(E_ALL);
ini_set('display_errors', 1);*/
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
// require 'vendor/autoload.php';
//path from index of API
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/SMTP.php';
/**
* This function is used to send email;
* $to is an array
*/
function sendMail($recipientEmail = array(), $recipientCCEmail = array(), $recipientBCCEmail = array(), $recipientName = array(), $subject, $body, $attachments = array(), $smtpServer,
$smtpUsername, $smtpPassword, $senderEmail,
$senderName = '', $senderReplyToEmail, $senderReplyToName = '') {
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
// $mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $smtpServer; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
// $mail->Port = 587; // TCP port to connect to
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($senderEmail, $senderName);
for ($i = 0; $i < count($recipientEmail); $i++) {
$mail->addAddress($recipientEmail[$i], isset($recipientName[$i]) ? $recipientName[$i] : ''); // Name is optional $mail->addAddress('email', 'name');
}
if (is_array($recipientCCEmail))
for ($i = 0; $i < count($recipientCCEmail); $i++) {
if ($recipientCCEmail[$i] != '')
$mail->addCC($recipientCCEmail[$i]); // Name is optional $mail->addAddress('email', 'name');
}
if (is_array($recipientBCCEmail))
for ($i = 0; $i < count($recipientBCCEmail); $i++) {
if ($recipientBCCEmail[$i] != '')
$mail->addBCC($recipientEmail[$i]); // Name is optional $mail->addAddress('email', 'name');
}
if (is_array($senderReplyToEmail))
for ($i = 0; $i < count($senderReplyToEmail); $i++) {
if ($senderReplyToEmail[$i] != '')
$mail->addReplyTo($senderReplyToEmail, $senderReplyToName);
}
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
if (is_array($attachments)) {
foreach ($attachments as $attachment) {
if ($attachment != '')
$mail->addAttachment($attachment, basename($attachment));
}
}
$mail->AltBody = strip_tags($mail->Body); // in case if the client does not support html
$mail->send();
// echo 'Message has been sent';
return true;
} catch (Exception $e) {
// echo 'Message could not be sent.';
// echo 'Mailer Error: ' . $mail->ErrorInfo;
return false;
}
}
?>