Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to Send Email from a PHP Script Using SMTP Authentication
Old 08-02-2011, 03:01 PM How to Send Email from a PHP Script Using SMTP Authentication
Novice Talker

Posts: 7
Trades: 0
I've been trying to muscle my way to figure out how to fix my form to send email via a PHP form and have received the following errors:


Notice: Undefined variable: smtp in C:\..\contact.php on line 65

Fatal error: Call to a member function send() on a non-object in C:\..\contact.php on line 65

Line 65: $mail = $smtp->send($to, $subject, $message, $additionalHeaders);

I'm not sure how to fix it, but I've supplied my code below:
<?php
// set flag to indicate whether mail has been sent
$mailSent = false;
if (
array_key_exists('eList', $_POST)) {
// mail processing script
// remove escape characters from POST array
if (get_magic_quotes_gpc()) {
function
stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return
$value;
}
$_POST = array_map('stripslashes_deep', $_POST);
}

$email = $_POST['email'];

// check for valid email address
$pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!
preg_match($pattern, trim($email))) {
$error['email'] = 'Please enter a valid email address';
}

// validate the input, beginning with name
$name = trim($_POST['name']);
if (empty(
$name)) {
$error['name'] = 'Please enter your First Name';
}
$lname = trim($_POST['lname']);
if (empty(
$lname)) {
$error['lname'] = 'Please enter your Last Name';
}
$department = $_POST['department'];
if (
$_POST['department'] == '') {
$error['department'] = 'Please select a Department';
}

// check the content of the text area
$messageBody = trim($_POST['message']);
if (empty(
$messageBody)) {
$error['message'] = 'Please enter your message';
}


// initialize variables
if (!empty($_POST['url'])) {
$to = 'email@email.com';//
$subject = 'Suspected as SPAM';
$host = 'smtpout.secureserver.net';
$username = 'username;
$password = '
password';
} else {
$to = '
email@email.com';//
$subject = '
Subject';
$host = '
smtpout.secureserver.net';
$username = '
username';
$password = '
password';
}

$SpamErrorMessage = "No URLs permitted";
if (preg_match("/http/i", "$name")) {echo "$SpamErrorMessage"; exit();}
if (preg_match("/http/i", "$lname")) {echo "$SpamErrorMessage"; exit();}
if (preg_match("/http/i", "$email")) {echo "$SpamErrorMessage"; exit();}
if (preg_match("/http/i", "$messageBody")) {echo "$SpamErrorMessage"; exit();} // check for spam

$mail = $smtp->send($to, $subject, $message, $additionalHeaders);

//build the message
$smtp = Mail::factory('
smtp',
array('
host' => $host,
'
auth' => true,
'
username' => $username,
'
password' => $password));
$message = "To: $department\r\n\r\n";
$message .= "From: $name $lname\r\n";
$message .= "$email\r\n\r\n";
$message .= "Question/Comment: $messageBody";

//build the additional headers
$additionalHeaders = "From: Contact <email@email.com>\r\n";
$additionalHeaders .= "Reply-To: $email";



//send the email if there are not errors
if (!isset($error)) {
$mailSent = mail($to, $subject, $message, $additionalHeaders);
// check that the mail was sent successfully
if (!$mailSent) {
$error['
notSent'] = 'Sorry, there was a problem sending your mail. Please try later.;
}
}
}
?>

Thank you to anyone who can help!
toad78 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-02-2011, 03:15 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Try moving the //build the message section above the $mail. Currently $mail is referencing a variable that doesn't yet exist.

So instead of this:

PHP Code:
$mail $smtp->send($to$subject$message$additionalHeaders);

//build the message
$smtp Mail::factory('smtp',
array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$message "To: $department\r\n\r\n";
$message .= "From: $name $lname\r\n";
$message .= "$email\r\n\r\n";
$message .= "Question/Comment: $messageBody"
Try this:

PHP Code:
//build the message
$smtp Mail::factory('smtp',
array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$message "To: $department\r\n\r\n";
$message .= "From: $name $lname\r\n";
$message .= "$email\r\n\r\n";
$message .= "Question/Comment: $messageBody";

$mail $smtp->send($to$subject$message$additionalHeaders); 
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 08-02-2011 at 03:17 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 04:24 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Novice Talker

Posts: 7
Trades: 0
Thanks PhysicsGuy, but it seems to have generated a new error:
Fatal error: Class 'Mail' not found in C:\..\contact.php on line 68

Line 68 is that very same line from the previous error message.
toad78 is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 04:42 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Where is the Mail class? What the script is doing is trying to use a (static) method named 'factory' in class named 'Mail'. Is there a Mail class anywhere? Maybe you forgot to require/include it.

Also, looking back I think I spotted an error in what I previously posted:

The replacement should be from this:

PHP Code:
$mail $smtp->send($to$subject$message$additionalHeaders);

//build the message
$smtp Mail::factory('smtp',
array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$message "To: $department\r\n\r\n";
$message .= "From: $name $lname\r\n";
$message .= "$email\r\n\r\n";
$message .= "Question/Comment: $messageBody";

//build the additional headers
$additionalHeaders "From: Contact <email@email.com>\r\n";
$additionalHeaders .= "Reply-To: $email"
To this:
PHP Code:
//build the message
$smtp Mail::factory('smtp',
array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$message "To: $department\r\n\r\n";
$message .= "From: $name $lname\r\n";
$message .= "$email\r\n\r\n";
$message .= "Question/Comment: $messageBody";

//build the additional headers
$additionalHeaders "From: Contact <email@email.com>\r\n";
$additionalHeaders .= "Reply-To: $email";

$mail $smtp->send($to$subject$message$additionalHeaders); 
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 08-02-2011 at 04:44 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 04:49 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Novice Talker

Posts: 7
Trades: 0
To be truthful, I found this recommendation online: How to Send Email from a PHP Script Using SMTP Authentication and have been trying to adapt it to my own code.
toad78 is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 04:56 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah, that could be your problem. Could you please post the link to the tutorial? The writer might be using another file, which I'm assuming would be where the Mail class is.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 04:59 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Novice Talker

Posts: 7
Trades: 0
I would except the forum won't allow me. Do a Google Search on the term How to Send Email from a PHP Script Using SMTP Authentication. It is on About.com.

Thank you Physicsguy for your time. I really do appreciate some education in this.
toad78 is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 05:06 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah, no problem

I found the article, and the first thing it says it so make sure PEAR Mail is installed. My guess is that's where the Mail class comes from

I also see the code has
PHP Code:
require_once "Mail.php"
in it. There's no way of me knowing where your files are, but you probably need to have some sort of Mail.php. Perhaps the PEAR Mail package gives you that.

Why do you need SMTP Authentication? The mail() function (which is built right into PHP) works just fine for me, and it even supports HTML. Why go to this trouble?
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 05:10 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Novice Talker

Posts: 7
Trades: 0
Believe me, I know the form works fine! It works fine on three other servers as well when I tested it. What happened was there was a server update on a client's server and when it was updated, the form quit working. It was suggested by their server maintainer that I create an SMTP Authentication login to see if that will work---just something else to figure out, I guess.

I looked on their server and don't see anything relating to a PEAR Mail script, so I'm not sure what else to do to get this working correctly. I'm open to suggestions!
toad78 is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 07:24 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
If you are allowed to add PHP PECL extensions, then you can just install this plugin to the server. If you can't... Well there's no way to get it working on that server

Sorry, but I'm glad to have (somewhat) helped!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 07:50 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Novice Talker

Posts: 7
Trades: 0
I removed all of the excess code for PEARL and added this to the form and it worked like a peach!

<?php
ini_set("sendmail_from", "info@domain.com");
?>

Thanks for your time and trying!
toad78 is offline
Reply With Quote
View Public Profile
 
Old 08-02-2011, 08:03 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Quote:
Originally Posted by toad78 View Post
I removed all of the excess code for PEARL and added this to the form and it worked like a peach!

<?php
ini_set("sendmail_from", "info@domain.com");
?>

Thanks for your time and trying!
No, thank YOU for posting a solution! Now people who google for this will know what you did
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-03-2011, 06:22 PM Re: How to Send Email from a PHP Script Using SMTP Authentication
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by toad78 View Post
I removed all of the excess code for PEARL and added this to the form and it worked like a peach!

<?php
ini_set("sendmail_from", "info@domain.com");
?>

Thanks for your time and trying!
So your original problem was most likely the lack of a sender address using the mail() function. In the fourth parameter of the mail() function you can specify additional headers, amongst them the "From" field. Your server wouldn't allow you to send mails because there was no sender address given.

What you did, was to add a default address to PHP's configuration file, to be used when no address is specified. Just adding it in the fourth parameter would have worked just as fine, as in:
PHP Code:
// ...
// set your to, subject and message fields as before
// ...

$headers 'From: "Your name" <your_email@domain.com>';

mail($to$subject$message$headers); 
Of course, either way works, so no reason for you to change it any more. Just thought I'd add some clarification
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 08-03-2011 at 06:23 PM.. Reason: typo
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to How to Send Email from a PHP Script Using SMTP Authentication
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41041 seconds with 12 queries