Another solution i've been searching for days now is for sending mail to actually existing accounts, but haven't quite found it yet. I have this functionality in my code where i can search and select professors individually for setting question papers. The search and select pages are working fine, so is the page where i assign them login credentials for setting papers. Now, since i(as admin) am assigning them the login credentials, i need to communicate that information to the selected professors. For this, i figured i'd mail them. I've set up a valid gmail account from where i want this mail to be sent. But my sendmailselected.php file is not working and is erroneous. I will take care to prevent sql injection so that is no issue. Also note that i have made changes in my php.ini file as required, and also have sendmail.zip and sendmail-SSL.zip folders extracted in the bin folder of wamp. Can you please point out errors and help me out. Here's the code:

<?php
include('connectionfile.php');
session_start();
if (!isset($_SESSION["useradmin"])) { 
    header("Location: index.html");
}


echo "<br><p>";

$prof_id = $_POST['prof_id'];
$temp_id = $_POST['temp_id'];
$temp_usname=$_POST['temp_uname'];
$temp_pswd = $_POST['temp_pwd'];
$temp_status = $_POST['status_paper'];
$sname= $_POST['sname'];
$my= $_POST['my'];



$insert_temp = "INSERT INTO temporary (prof_id, temp_id, temp_uname, temp_pwd, subject_name, monthandyear, status) VALUES ( '".$prof_id."', '".$temp_id."', '".$temp_usname."', '".$temp_pswd."', '".$sname."', '".$my."', '".$temp_status."')";

mysql_query($insert_temp) OR die (mysql_error());


$profmail= "select prof_email_id from professor where prof_id= $prof_id ";

$gotid = mysql_query($profmail) OR die (mysql_error());

$message= "You have been selected for question paper setting for the examination to be held in $my for the subject $subj and your temporary login credentials are as follows: <br> Temporary Username: $temp_usname <br> Temporary ID: $temp_id <br> Temporary Password: $temp_pswd . Please see to it that you sign in and submit the question paper before 10 days. After this period, your temporary account will expire."



$messagelong = wordwrap($message, 3, "\n", true);
$messagelong = wordwrap($message);


ini_set(sendmail_from, ' my gmail account id here ');   

mail($profmail, "Selected for paper setting", $message);
ini_restore("sendmail_from"); 


echo("<p><br> Entry for $temp_id successfully inserted and mail sent.");

mysql_close($id_link);


?>

There is this link i referred to for changes to be made in the php.ini and sendmail.php files. If you need to see that, tell me, i'll post the link. Thanks! :)

Dani AI

Generated

Short diagnosis (what actually happened)

  • The PHP mail() call returning true just means PHP accepted the message for delivery — it does not guarantee delivery to the recipient. (php.net)
  • On Windows/WAMP mail() hands messages to the SMTP settings or a sendmail wrapper in php.ini; it does not natively handle the authenticated TLS/STARTTLS flow Gmail requires. Tweaking sendmail_from fixed the undefined-constant error that and pointed out, but it won’t make mail() talk authenticated TLS to smtp.gmail.com. (php.net)

Recommended fix (practical, minimal changes)

  • Stop trying to make raw mail() send through Gmail. Use a proper SMTP library (PHPMailer or similar), authenticate with Gmail and send over TLS. PHPMailer has built-in SMTP, debug output and examples for Gmail. (github.com)
  • If using a personal Gmail account you’ll need an App Password (or OAuth2) rather than your regular password (Google removed simple “less secure app” access). Set 2‑step verification then create an App Password and use it as the SMTP password. (learn.microsoft.com)

Minimal PHPMailer example

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@gmail.com';
$mail->Password = 'your_app_password'; // use App Password or OAuth2
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

$mail->setFrom('your@gmail.com','Uni Admin');
$mail->addAddress($profEmail);
$mail->isHTML(true);
$mail->Subject = 'Selected for paper setting';
$mail->Body = $messageHtml;
$mail->send();

(See PHPMailer docs for installation and SMTPDebug options.) (github.com)

Quick troubleshooting checklist

  • Turn on PHPMailer SMTPDebug to capture connection/auth errors. (github.com)
  • Confirm outbound port 587/465 isn’t blocked on the dev machine or network; test connectivity to smtp.gmail.com. (support.jamf.com)
  • For local/dev testing consider a mail-trap service (so you don’t spam real accounts).
  • Avoid hardcoding credentials; use environment variables or a secure vault. Replace deprecated mysql_* with PDO/MySQLi when you touch DB code.

Recommended Answers

All 13 Replies

my sendmailselected.php file is not working and is erroneous

What's not working, and what's erronous?

I get this when this file is executed:
Notice: Use of undefined constant sendmail_from - assumed 'sendmail_from' in sendmailselected.php on line 123.
Here those lines are:

ini_set(sendmail_from, ' my gmail account id here ');   
$sent = mail($profmailsend, "Selected for paper setting", $newtext, "From: <Uni Admin>");
ini_restore("sendmail_from");     

Hence the mail is not getting sent.

Now only this file doesn't work. Please help me solve my problem asap. Thanks!

Your sendmail_from should be in quotes.

tried doing that, still shows me a parse error on the same lines of code.

Member Avatar for Member #949455

Notice: Use of undefined constant sendmail_from - assumed 'sendmail_from' in sendmailselected.php on line 123.

Which line of code?

Is it this one:

ini_set(sendmail_from, 'my gmail account id here'); 

tried doing that, still shows me a parse error on the same lines of code.

From this:

ini_set(sendmail_from, 'your email address'); 

to this:

ini_set('sendmail_from', 'your email address'); 

Did you get the same error?

I notice the code you provided above on line 38 to line 41 doesn't have qoutes either put it too:

ini_set('sendmail_from', ' my gmail account id here ');
mail($profmail, "Selected for paper setting", $message);
ini_restore("sendmail_from");

Okay, now no errors. The mail function seems to be working fine. Tested it using the following lines of code:

if($sent) 
{print "<br>Your mail was sent successfully"; }
else 
{print "<br>We encountered an error sending your mail"; }

and when i execute it shows me 'Your mail was sent successfully'. But when i check my gmail acc to which the mail was sent to, i have not received any mail, neither in inbox nor in junk folders. Plus, i checked the admin acc too, and it shows 'no sent mails'. Is there anything else i left out in my coding? I even put my wamp server online before sending the mail... please assist me!

Member Avatar for Member #949455

But when i check my gmail acc to which the mail was sent to, i have not received any mail, neither in inbox nor in junk folders.

I am assuming in your database your email address is valid?

yes it is...

wamp server online before sending the mail

So you are running this from you development machine? Do you have a mail server installed?

Member Avatar for Member #949455

yes it is...

Like what pritaeas mention did you installed a mail server?

I think I forgot to ask that question in the beginning.

Member Avatar for Member #949455

Yes i guess

Do you have something like that or you don't?

If you don't take a look at these articles (instructions) on how to setup SMTP:

http://www.xtreemhost.com/2010/04/11/how-to-send-e-mails-via-smtp-with-phpmailer-and-gmail/

Take a look at these code snippets which has a SMTP files in it (both of these are tutorials about sending emails with SMTP):

http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

http://www.9lessons.info/2009/10/send-mail-using-smtp-and-php.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.