0

Basically I have my PHP send an email to the person who signed up so then they their email will be verified. I did a test run and the link is generated with my database, its just the email that isn't making its way to the inbox. I'm not sure if its a delay or its an issue with the code, but any help would be much appreciated.

PHP For Email To Be Sent:

<?php

include('config.php');

// table name 
$tbl_name=temp_members_db;

// Random confirmation code 
$confirm_code=sha1(uniqid(rand())); 

// values sent from form 
$name=$_POST['name'];
$email=$_POST['email'];

// Insert data into database 
$sql="INSERT INTO $tbl_name(confirm_code, name, email, password)VALUES('$confirm_code', '$name', '$email', '$password')";
$result=mysql_query($sql);

// if suceesfully inserted data into database, send confirmation link to email 
if($result){
// ---------------- SEND MAIL FORM ----------------

// send e-mail to ...
$to=$email;

// Your subject
$subject="Your confirmation link here";

// From
$header="from: Colourity <your email>";

// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.colourity.com/confirmation.php?passkey=$confirm_code";

// send email
$sentmail = mail($to,$subject,$message,$header);
}

// if not found 
else {
echo "Not found your email in our database";
}

// if your email succesfully sent
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}
?>
user3105267
  • 3
  • 1
  • 1
  • 7
  • Are you on shared hosting? – display-name-is-missing Dec 25 '13 at 02:02
  • where your trying to send the email from? your own server? (wamp or other kind), or from website server? –  Dec 25 '13 at 02:02
  • Have you checked out http://stackoverflow.com/questions/1658043/troubleshooting-php-mail ? – Tim Dearborn Dec 25 '13 at 02:04
  • @DanielLisik Yes, I am. – user3105267 Dec 25 '13 at 02:06
  • @user3052629 From my web server – user3105267 Dec 25 '13 at 02:07
  • In general mails should arrive quite quickly if not instantaneously. Have you checked your SPAM folder? If your mail is not getting through, it is because your host doesn’t have `localhost` mail setup or allows it for your scripts. So you need to use a PHP library that sends mail via SMTP to another mail account. – Giacomo1968 Dec 25 '13 at 03:10
  • @JakeGould I just disabled something called "SendMail" but it did not resolve my issue. In the log it says the email has been sent. Do you recommend any good PHP libraries? – user3105267 Dec 25 '13 at 03:16
  • 1
    Please read about [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) before putting this into production. Your database insert code could be abused by attackers to insert random data into your database, steal passwords, or even erase your entire database completely. Also, are you encrypting your passwords at all? – nhinkle Dec 25 '13 at 04:01
  • Yeah, I'm surprised that it took this long to mention SQL injection. Also, that you should be using mysqli or PDO. – JAL Dec 25 '13 at 05:59
  • @user3105267 are you testing this program on YOUR OWN LOCALHOST server or on a real web host? If real web host, what is the name of the host? – itsols Dec 25 '13 at 05:59
  • @itsols I'm testing it on a real host. Its called Hostinger. I'm using that paid version, if that makes any difference. – user3105267 Dec 25 '13 at 17:07
  • @nhinkle That was a step I was going to implement later on. And I am also encrypting my passwords. :) – user3105267 Dec 25 '13 at 17:08
  • @user3105267 don't wait until later. Do it right from the start, or you will miss it somewhere and you will get compromised. – nhinkle Dec 25 '13 at 17:42
  • @nhinkle Will I have to do it for the whole thing or just the form part? – user3105267 Dec 25 '13 at 17:46
  • @user3105267 anywhere you have any SQL, you will need to fix it to make sure there's no way it can be abused. Any time you accept any type of user input, be it in a form, a URL parameter, etc. you need to treat the input as dangerous and handle it properly. – nhinkle Dec 25 '13 at 18:10
  • @nhinkle So I have fixed the issue with sql injection and moved over to mysqli, but I am getting error(s) when I submit the form. To show all the issues I have uploaded it to a online notepad. http://notes.io/xHM – user3105267 Dec 25 '13 at 18:36
  • @user3105267 Yes, it makes a difference. If it's your own local host you generally will never get the mail function working as the mail server isn't setup. As for free hosts, they *usually* disable mailing functions - that's how they make their money. But if it's paid, yes, generally you have it all setup for you and your function must work. But then, in certain cases (I mean servers), you ought to include the sender and reply-to addresses as well in the mail header. This is a prerequisite by the host to prevent spam and fake mails. – itsols Dec 26 '13 at 03:04
  • @itsols I figure out that I needed to take care if the sender and reply-to. Thanks for the help! – user3105267 Dec 27 '13 at 00:58

3 Answers3

0

You can use a PHP class like HTML Mime Mail (RMail) to act like a SMTP client to send your email. You will need a valid email account to use and your web server will still need to be able to send data out to whatever port you email account requires (usually port 25, 465, or 587).

Tim Dearborn
  • 1,178
  • 7
  • 18
  • Are you aware of how long it will take for the email to receive the user? – user3105267 Dec 25 '13 at 02:10
  • 1
    It will be as fast as the email server your email account uses. Basically, it will be the same as if you used your own email application to send an email. – Tim Dearborn Dec 25 '13 at 02:13
0

I would recommend SwiftMailer. Instructions on using SMTP—to connect to another account to mail it—is located here.

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);

But all that said, I would investigate if your host can handle localhost mailing since that is the easiest to use if you have that available.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

I would recommend PHPmailer Library. Very easy to use for sure.

https://github.com/Synchro/PHPMailer