I have a delete query below from a php page where the user types in an extension number and the entry is deleted from the database.
Name,Email,Extension,Phone,Department the whole line is deleted by entering the extension number of the database member.
Now I have phpmailer installed and working for this, it is sending a mail to IT#sadsa.com but I am trying to make it also send the mail to another address before the member is deleted from the database depending on the department they are in.
Eg. if they are in Sales department then the email must be sent to sales#sadsa.com and IT#sada.com, if they are in the Admin Department then the mail must be sent to Admin#sadsa.com and IT#asdas.com.
My current code below :
delete.php (the php page where the user enters the extension number to delete)
<?php
require ("database.php");
session_start();
if (!isset($_SESSION['CheckLogin'])) { header("Location: login.php"); }
$this_user_ext =$_REQUEST['extension'];
// sending query
mysql_query("DELETE FROM users WHERE extension = '$this_user_ext'")
or die(mysql_error());
if($_POST['action']) {
include('maildelete.php');
$extension=$_POST['extension'];
header("Location: index.php");
}
?>
<center><form action="" method="post">
Enter 4 Digit Extension Number :<br><input type="text" name="extension"><br><input type="submit" name="action" value="Delete!">
<br> <br>
<h3>
Main Menu
</h3>
</form>
</center>
maildelete.php (this is the script that sends the mail to IT#sad.com)
<?php
$extension = $_POST['extension'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.titan-networks.co.za"; // SMTP server // enables SMTP debug information
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = false;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.titan-networks.co.za"; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "jurgen#titan-networks.co.za"; // SMTP account username
$mail->Password = "****"; // SMTP account password
$mail->From = "no-reply#alpinemotors.co.za";
$mail->FromName = "Extension List";
$mail->AddAddress('jurgen#titan-networks.co.za', "");
$mail->isHTML(true);
$mail->Subject = 'Extension Deleted';
$mail->Body = "Extension Number " . $extension . " was removed from the Extension List";
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Email Sent Successfully!';
?>
Related
Constructing Forgot UserName Page:
Withing php file is getting hosted under localhost xampp.
Using Gmail email address to send mail to another gmail address with username
No error, just mail is not sent.
I added the following files from Github inside the includes directory.
PHPMailerAutoload.php,
class.phpmailer.php,
class.smtp.php,
class.phpmaileroauth.php,
class.phpmaileroauthgoogle.php,
class.pop3.php
Then inside includes directory created "forgot_username_inc.php" UPDATED
Here is the php code for it
<?php
include_once 'db_connect.php';
include_once 'psl-config.php';
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
require_once 'class.phpmaileroauth.php';
require_once 'class.phpmaileroauthgoogle.php';
require_once 'class.pop3.php';
$error_msg_username = "";
if (isset($_POST['email'])) {
// Sanitize and validate the data passed in
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Not a valid email
$error_msg_username .= '<p class="error">The email address you entered
is not valid</p>';
}
$prep_stmt = "SELECT username FROM client WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);
// check existing email
if ($stmt) {
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
if (!($stmt->num_rows == 1)) {
// If no user with such email
$error_msg_username .= '<p class="error">No User Registered with that Email</p>';
$stmt->close();
}
}
else {
$error_msg_username .= '<p class="error">Database error Line 39</p>';
$stmt->close();
}
if (empty($error_msg_username)) {
if($stmt_username = $mysqli->prepare("SELECT username FROM *** WHERE email = ? LIMIT 1")) {
$stmt_username->bind_param('s', $email);
$stmt_username->execute();
$stmt_username->store_result();
$stmt_username->bind_result($username);
// user found
if ($stmt_username->num_rows == 1) {
// sending mail
$mail = new PHPMailer;
$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->Port = 587; // set the SMTP port for the GMAIL
server
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption,
'ssl' also accepted
$mail->SMTPDebug = 2;
$mail->From = 'example#gmail.com';
$mail->FromName = 'Admin';
$mail->addAddress($email); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50
characters
$mail->isHTML(true); // Set email format to
HTML
$mail->Subject = 'User Name Forgot';
$mail->Body = 'Here is your UserName'.$username;
$mail->AltBody = 'Here is your UserName'.$username;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
//---End of Mail
} // end of userfound
} // end of prepare statement
if (! $stmt_username->execute()) {
header('Location: ../error.php?err=Username Search: FAILED');
exit();
}
$stmt_username->close();
header('Location: ./forgot_username_success.php');
exit();
}
}
?>
Error it shows:
Please log in via your web browser Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
What I did: Disable 2-way authentication.Allow less secure apps access.
clear browser cache. I also "unlock CAPTCHA"
https://accounts.google.com/DisplayUnlockCaptcha
Still failed.
UPDATE:
I used GMX instead of gmail & now it sending mails successfully.
Once my code is run in PHP, it says that the confirmation mail has been sent successfully, but the mail has not been received at the target mail ID. I have used the mail() function in PHP to send the confirmation mail and I have also installed Postfix on my Ubuntu. What is the problem here?
<?php
include('config.php');
$tb_name = temp_members_db;
$confirm_code = md5(uniqid(rand()));
$name = $_POST['name'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$country = $_POST['country'];
$sql = "INSERT INTO $tb_name(confirm_code,name,email,password,country) VALUES ('$confirm_code','$name','$email','$pass','$country')";
$result = mysql_query($sql);
if($result) {
$to = $email;
$sub = "Your Confirmation Code";
$message = "Your confirmation code is" . $confirm_code;
$send = mail($to,$sub,$message);
var_export($send);
} else {
echo "Havent found email ID in our database";
}
if($send) {
echo "Sent the confirmation link to your email ID";
} else {
echo "Sending failed";
}
?>
Have you switched on the SMTP function on the localhost server?
If not enabled the SMTP and mail functions
Use PHPMailer class. Very easy to install and use. The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn't include a local mail server; PHPMailer's integrated SMTP implementation allows email sending on Windows platforms without a local mail server.
Just download the class files from here: https://github.com/PHPMailer/PHPMailer
Example:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
Set SMTP and if you want to use secure encryption (ssl,tls and ports)
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // If you want to use encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to(or port 25,465 etc)
Set fields like from, to, bcc, subject, email body etc.
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
// Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I am trying to send email from my website through PHP Mailer class in PHP.
The code is given below but when I am trying to send the email, I am getting these two errors:
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
What is going wrong. Please help?
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer#bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_myemail#host.com"; // SMTP username
$mail->Password = "password"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("myemail#host.com", "namit pathak");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
You need to include your email's username and password on this line, currently you are using the original dummy settings:
$mail->Username = "send_from_myemail#host.com"; // SMTP username
$mail->Password = "password";
you only put the host as localhost at a time you must configured your local smtp server
$mail->Host = "localhost";
if you have google accounts means try it
$mail->Host = "smtp.google.com";
I am using PHPMailer to create Contact form. It is working fine. But When I reply to received message, user is receiving my reply include with previous data.
owner_address#gmail.com = admin Email Address.
User Name = Email sender from Contact Form.
On Wed, Jul 30, 2014 at 1:35 AM, User Name<owner_address#gmail.com> wrote:
First Email Data
It is showing user name correctly, but it is showing my email address (not the user's email)
I want to remove email address or change it to user address.
Below is my code;
<?php
if (!isset($_POST['your_name']))
{
return false;
}
$name = $_POST['your_name'];
$email = $_POST['your_email'];
$subject = $_POST['your_subject'];
$msg = $_POST['description'];
include "PHPMailerAutoload.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "owner_address#gmail.com";
$mail->Password = "owner_password";
$mail->addReplyTo($email, $name);
$mail->SetFrom($email, $name);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AddAddress("owner_address#gmail.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "OK";
}
?>
Thank you,
Sameera Silva
I am trying to send user a activation link through mail by using my gmail account. how do i set it up.How do Send email using Gmail? Where do I put the password?.
Is it to ancient or should I go for object oriented method.
// secure the password
$passWord = sha1($passWord);
$repeatPass = sha1($repeatPass);
// generate random number
$random =rand(1200345670,9999999999);
//send activation email
$to = $email;
$subject = "Activate your account";
$headers = "From: ti.asif#gmail.com";
$server = "smtp.gmail.com";
$body = "Hello $username,\n\n You registered and need to activate your account. Click the link below or paste it into the URL bar of your browser\n\nhttp://phpacademy.info/tutorials/emailactivation/activate.php?id=$lastid&code=$code\n\nThanks!";
ini_set("SMTP",$server);
if (!mail($to,$subject,$body,$headers))
echo "We couldn't sign you up at this time. Please try again later.";
else
{
// register the user
$queryreg = mysql_query("
INSERT INTO users VALUES ('','$userName','$passWord','$fullName','$date','$random','0','$email')
");
$lastid = mysql_insert_id();
die ("You have been registered. <a href='login.php'>Click here</a> to return to the login page.");
echo "Successfully Registered";
}
Download phpmailer and try the following code
<?php
$mail = new PHPMailer();
$mail->IsSMTP();
//GMAIL config
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "gmailusername"; // GMAIL username
$mail->Password = "gmailpassword"; // GMAIL password
//End Gmail
$mail->From = "from#email.com";
$mail->FromName = "you name";
$mail->Subject = "some subject";
$mail->MsgHTML("the message");
//$mail->AddReplyTo("reply#email.com","reply name");//they answer here, optional
$mail->AddAddress("address#to.com","name to");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {//to see if we return a message or a value bolean
echo "Mailer Error: " . $mail->ErrorInfo;
} else echo "Message sent!";
the mail builtin is not very suitable for this, it supports only simple setups.
have a look at pear mail, the examples show you how to send using smtp auth.