Jump to content

Should this send mail?


Johnnyboy123

Recommended Posts

I'm trying to send email with php for the first time. Trying to make it so that after a user registers he/she receives an email after submitting. I'm trying to send it from 1 comp with an email of goldie@telkomsa.net to another with an email of jgoldie@telkomsa.net (I just really want to test if it's sending email another before I get technical with the email)

 

Should this code work, here is part of my code with the email section in it:

<?php
// query
$sql = "INSERT INTO student (sno, sname, init, fname,
						title, msname, dob, sex, lang, idno,
						telh, telw, cel, fax, email, address, 
						contact_flag
						)



	VALUES 				('', '$sname', '$init', '$fname', 
						'$title', '$msname', '$dob', '$sex','$lang',
						'$idno', '$telh', '$telw', '$$cell', '$fax',
						'$email', '$address', '$contact')";




mysql_query($sql) or die('Error:' . mysql_error());
	  
$sno_id = mysql_insert_id();
	  


	// get the cid
$cname = mysql_real_escape_string($_POST['cname']); 
$getCID = "SELECT cid FROM course WHERE cname='$cname'";
$result = mysql_query($getCID);

if($result)
{
    $row = mysql_fetch_assoc($result);
    $course_id = $row['cid'];

     // add the student to the course_student table
     $addCID = 'INSERT INTO course_student (cid, sno) VALUES(' . $row['cid'] . ', ' . $sno_id . ')';
     mysql_query($addCID) or die('Error:' . mysql_error());

// send email
$Name = "Da Duder"; //senders name
$email = "goldie@telkomsa.net"; //senders e-mail adress
$recipient = "jgoldie@telkomsa.net"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

ini_set('sendmail_from', 'goldie@telkomsa.net'); //Suggested by "Some Guy"

if (mail($recipient, $subject, $mail_body, $header)){ 
echo "mail has been sent"; //mail command 
}



}



	  
	 header( "refresh:5;student_man.php" );
echo 'Registration <b> successful </b> You\'ll be redirected in about 5 secs. If not, click <a href="student_man.php">here</a>.';


	}
} 


?>

 

Email is under the "email section" comment based of what I found on google. I'm not getting any errors so I want to know if the email isn't going through because of my code or my smtp setting etc.

Link to comment
Share on other sites

With all things: the proof of the pudding is in the eating, (om nom nom) so test it and see if you get errors. To enable error reporting

add the following above your script:

error_reporting(E_ALL);
ini_set("display_errors", 1);

remove afterwards, on a remote host.

 

also in I noticed you had a double $ i you values part of the sql query ( '$$cell' )

Apart from that do you run this local or on a remote server. If it's local host you might want to change some smtp setting in your php.ini

this is what i have

[mail function]
; For Win32 only.
; http://php.net/smtp   was localhost
SMTP = smtp.yourcablecompany.com
; http://php.net/smtp-port
smtp_port = 25

So you need to check out what the settings are of your provider.

 

No need to do this of course when you have a paid remote host.

Link to comment
Share on other sites

Thanks alot. After changing the smtp and port details I received this error:

Warning: mail() [function.mail]: Failed to connect to mailserver at "my smtp details" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Program Files\EasyPHP-5.3.3\www\Project\registerstudent.php on line 132

 

<?php
if($result)
{
    $row = mysql_fetch_assoc($result);
    $course_id = $row['cid'];

     // add the student to the course_student table
     $addCID = 'INSERT INTO course_student (cid, sno) VALUES(' . $row['cid'] . ', ' . $sno_id . ')';
     mysql_query($addCID) or die('Error:' . mysql_error());

// send email
$Name = "Da Duder"; //senders name
$email = "goldie@telkomsa.net"; //senders e-mail adress
$recipient = "jgoldie@telkomsa.net"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

ini_set('sendmail_from', 'goldie@telkomsa.net'); //Suggested by "Some Guy"

mail($recipient, $subject, $mail_body, $header);                          //Line 132

}

}
}
?>

 

 

What do I do now to correct this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.