-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.php
99 lines (81 loc) · 3.39 KB
/
mailer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
session_start();
$city= $_SESSION['userDetails']['city'];
// if($_SESSION['loggedin']==true)
// {
// $name= $_SESSION['userDetails']['name'];
// $address= $_SESSION['userDetails']['address'];
// }
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
//Database Connection
$servername="localhost";
$username="root";
$password="";
$dbname="electrothon";
$conn=new mysqli($servername,$username,$password,$dbname);
$type=$_POST['type'];
if($conn-> connect_error)
{
die("Connection failed".$conn-> connect_error);
}
else{
$sql="SELECT email FROM donor WHERE type='$type' && dcity='$city'";
$result=$conn->query($sql);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc()){
$email=$row['email'];
echo $email;
sendMail($email);
}
}
}
// try{
function sendMail($email) {
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$name= $_SESSION['userDetails']['name'];
$address= $_SESSION['userDetails']['address'];
$type=$_POST['type'];
//Fetch receiver's details
// $rname=$_POST['rname'];
// $number=$_POST['number'];
// $type=$_POST['type'];
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'Your e-mail here'; // SMTP username
$mail->Password = 'Your password here'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption, `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]');
//$mail->addAddress('[email protected]', 'Joe User'); // Add a recipient
$mail->addAddress($email); // Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
// 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 = 'Blood needed urgently !';
// $mail->Body = $bgroup." blood needed urgently.Please contact ".$rname." at ".$number." as soon as possible.";
$mail->Body =$type." blood needed ugently in ".$name." hospital at ".$address." .Please contact them as soon as possible.";
$mail->Body = "Hello ! This is to inform you that ".$type." blood is required in ".$name." at ".$address." .Please donate as soon as possible.Thank You.";
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header('Location: blood_bank.php');
}
// }
// catch (Exception $e) {
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
?>