I am trying to make a registration page with an email activation form using the php mailer library. After filling out the form and submitting the page I receive I don't receive any errors. I've checked my email multiple times and I don't seem to be receiving any emails. Since there are no errors, I'm not sure what else to look for. Has anyone ever had a similar problem before? Could it be an issue from gmail?
Heres my Send_mail.php
<?php
function Send_Mail($to,$subject,$body)
{
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$from = "batoosay#gmail.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.gmail.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "batoosay#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
And here is my index.php
<?php
include 'db.php';
$msg='';
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) )
{
// username and password sent from form
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'");
// email check
if(mysqli_num_rows($count) < 1)
{
mysqli_query($connection,"INSERT INTO users(email,password,activation) VALUES('$email','$password','$activation')");
// sending email
include 'Send_Mail.php';
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, please activate email.";
}
else
{
$msg= 'The email is already taken, please try new.';
}
}
else
{
$msg = 'The email you have entered is invalid, please try again.';
}
}
// HTML Part
?>
BTW it loads for a very long time.
Please base your code on the gmail example provided with PHPMailer, as it is supplied with correct settings.
The syntax you've used in the Host property is fine (though not as common as setting SMTPSecure = 'tls'), but you're using the wrong port to use STARTTLS (which is what it uses when you specify tls) - you need to use Port = 587. This is probably what is causing your delay. If that doesn't work, read the troubleshooting guide.
Related
I have this problem where when I receive an email sent from my localhost website, I get the wrong email address for the for the sender section ($mail->SetFrom($email, $name)). I get my own email address as the sender and not the one inputed in the text box on my website.
I've looked everywhere for some answers, sadly nothing worked. I've tried going on Chrome Account Settings and setting the less secure apps to ON. That didn't work.
I've tried multiple ways of setting the SetFrom email and name. NEED HELP!
<?php
$dir = __DIR__;
require_once("$dir/../PHPMailer-master/PHPMailerAutoload.php");
extract($_POST, EXTR_PREFIX_ALL, "P");
$name = $_POST['postName'];
$email = $_POST['postEmail'];
$subject = $_POST['postSubject'];
$message = $_POST['postMessage'];
$file = $_POST['postFile'];
echo "Name: ".$_POST['postName'];
echo "\n";
echo "Email: ".$_POST['postEmail'];
echo "\n";
echo "Subject: ".$_POST['postSubject'];
echo "\n";
echo "Message: ".$_POST['postMessage'];
echo "\n";
echo "File: ".$_POST['postFile'];
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.gmail.com"; // SMTP server
//$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxx#gmail.com"; // GMAIL username
$mail->Password = "xxxx"; // GMAIL password
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email, $name);
$mail->addAddress("xxxx#gmail.com", "name");
$mail->AddAttachment("$file");
$mail->Subject = "$subject";
$mail->Body = "$message";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
} ?>
This is an alert I set up showing all the POST parameters sent to the my PHP script. All the POST variables are there. The only problem is the SetFrom($email, $name)
Javascript Alert with POST Parameters
Gmail does not allow you to set arbitrary from addresses, though you can define preset aliases.
However, you shouldn't be trying to do this anyway. Putting user-provided value in the from address is a very bad idea as your messages will fail SPF checks because it's forgery. Put your own address in the from address (as well as the to address), and put the submitter's address in a reply-to header using the addReplyTo() method. You can see this working in the contact form example provided with PHPMailer:
//Use a fixed address in your own domain as the from address
//**DO NOT** use the submitter's address here as it will be forgery
//and will cause your messages to fail SPF checks
$mail->setFrom('from#example.com', 'First Last');
//Send the message to yourself, or whoever should receive contact for submissions
$mail->addAddress('whoto#example.com', 'John Doe');
//Put the submitter's address in a reply-to header
//This will fail if the address provided is invalid,
//in which case we should ignore the whole request
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
...
I've been trying to implement a contact service using Mailgun coded in PHP. I receive the following error:
Could not connect to SMTP host
Below is my code:
<?php
require("../includes/config.php");
require("../mailgun-php/vendor/autoload.php");
require("../phpmailer/_lib/class.phpmailer.php");
use Mailgun\Mailgun;
if($_SERVER["REQUEST_METHOD"] === "GET")
{
render("contact-form.php", ["title" => "Contact us"]);
}
if($_SERVER["REQUEST_METHOD"] === "POST")
{
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailgun.org'; // Specify mailgun SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'postmaster#sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // SMTP username from https://mailgun.com/cp/domains
$mail->Password = '*SMTP password from sandbox domain'; // SMTP password from https://mailgun.com/cp/domains
$mail->SMTPSecure = 'sll'; // Enable encryption, 'ssl'
$mail->Port= '465';
$mail->From = 'sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // The FROM field, the address sending the email
$mail->FromName = 'Enquiry bot'; // The NAME field which will be displayed on arrival by the email client
$mail->addAddress('****#gmail.com'); // Recipient's email address and optionally a name to identify him
$mail->isHTML(true); // Set email to be sent as HTML, if you are planning on sending plain text email just set it to false
// The following is self explanatory
$mail->Subject = 'Client enquiry';
$mail->Body = $_POST["message"];
if(!$mail->send())
{
echo "Message hasn't been sent.";
echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
}
else
{
redirect("confirmation.html");
}
}
?>
Note that this is the controller (I'm using MCV).
This is part of a website I've been implementing as a project for a CS class. Therefore I would avoid buying a private domain.
UPDATE: I've modified the original post with how this piece of code should be. May anyone else encounter this issue, this snippet is working without any issue.
I've contacted the Mailgun staff which provided me with excellent support. The problem lies with how I specify the port. It should be 465 for SSL and 587 for TLS.
I'm working on a registration page on a website where I need to implement a email verification.
I have installed PHPMailer-master on the server. The first part of the code is written directly in the registration php:
include('send_mail.php');
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> thank you for joining us! Please verify your email and get started using your account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, a verification email has ben sent to you. Please activate this to get started.";
And the code in send_mail.php:
<?php
function Send_Mail($to,$subject,$body)
{
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';
$from = "*mywebsite*";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "*my webhost's mailserver*"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "*username*"; // SMTP username
$mail->Password = "*password*"; // SMTP password
$mail->SetFrom($from, '*mywebsite?*');
$mail->AddReplyTo($from,'*mywebsite?*');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
I'm fairly sure the information in send_mail.php is correct. I'm using the information I found on my configure mail client page on cPanel ("Secure SSL/TLS Settings").
When I try to register, the website goes white somehow. The account gets created on phpmyadmin. No error-log in public_html.
Any ideas?
UPDATE: Sorry for late response.
I've tried using the PHPMailer Test Page. When I try SMTP, the page just loads forever. When using Sendmail, I get the error "Could not execute: /usr/sbin/sendmail -t -i". When I use the QMAIL, it says the email was sent but I never recieve it.
BUT... when I use Mail, I recieve it successfully. Is "Mail" secure enough to use? What are the differences? And how should I implement it into the php code of the register page? I can't find any specific part in the PHPMailer Test Page code for the "mail" function.
Code looks like this now:
$email = $_POST['email'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo('myemail');
$mail->AddAddress($email);
$mail->SetFrom('myemail');
$mail->AddReplyTo('myemail');
$mail->Subject = 'subject';
$mail->Message = 'message comes here';
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
Error message: "Message body empty"
**> the mail posted with the below given is going to spam. I am not using
captcha in the form as i don want to. So can anybody help me to the
mail in Inbox**
<?php
if(isset($_POST['submit']))
{
$name = $_POST['Name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$date = $_POST['checkinDate'];
$package = $_POST['package'];
$person = $_POST['adults'];
$kids = $_POST['kids'];
$ip=$_SERVER['REMOTE_ADDR']; //trace the ip address of the user submited
$subject ="Query From :Kerala-Honeymoon-Packages - Promotion\n"; //subject of the email
$to="paul#roverholidays.com";
$cc="online#roverholidays.com";
$ccc="deepti#roverholidays.com";
$from=$_POST['email'];
$adc="Name :$name\n";
$adc.="Email :$email\n";
$adc.="Phone :$phone\n";
$adc.="Date of Travel :$date\n";
$adc.="Package :$package\n";
$adc.="Adults :$person\n";
$adc.="Kids :$kids\n";
$message ="$name copy of the query you submited in Kerala-Honeymoon-Packages";//message header to user submited
$headers="From: <".$from. ">" ;
mail($cc,$subject,$adc,$headers);
mail($ccc,$subject,$adc,$headers);
mail($email,$message,$adc);
header("Location: thanks.htm");
}
else
{
return false;
}
?>
coding-wise I don't think there is anything you can do because it is the email server that classifies your email as a spam not the way you coded your script. All you can do is to control it from the receiver email setting i.e you setup your gmail filters to detect that email based on keyword like "Kerala-Honeymoon-Packages" and move it out of spam.
I don't know for sure what the email servers algorithms are for marking email as spam. However, I think sending email from different domain rather than your domain name is likely to be detected as phishing email. what I mean is when someone put his/her yahoo email in the form and click on send, your server will send the email to emails addresses in the script but it will send it as if it came from yahoo, which will be suspicious for the receiver email server as it knows that it did not come from yahoo.
Many email services block mail sent directly from random servers because they have little to no reputation as a legitimate source of non-spam emails. Instead of using the straight php mail() function, try using a SMTP service like Mandrill or Gmail's SMTP service. Both are free.
Here is the configuration page for Mandrill:
http://help.mandrill.com/entries/23737696-How-do-I-send-with-PHPMailer-
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MANDRILL_USERNAME'; // SMTP username
$mail->Password = 'MANDRILL_APIKEY'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Your From name';
$mail->AddAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->AddAddress('ellen#example.com'); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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;
exit;
}
echo 'Message has been sent';
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.