Click to See Complete Forum and Search --> : manually putting smtp on php script..how?
riskmod
01-29-2008, 10:13 AM
rookie question on php mail:
my job wants me to send a simple mail to some of our internal folks (about 1000 peeps). So I was going to do a simple php email and I was given an smtp server to relay...The thing is I know I would have to put the smtp path on the php.ini, but what I want to know if there was anyway you can manually put your smtp path on the php script without touching the ini file.
Where would the smtp go on the script??
EX of simple php mail:
<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Thanks
jasonahoule
01-29-2008, 11:04 AM
You can use the ini_set() function.
riskmod
01-29-2008, 11:46 AM
So just as long as you do it like this?:
<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
ini_set("smtpaddress",$fromaddress)
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
or something like this?:
$from = $fromname." <".$fromaddress.">";
ini_set("sendmail_from",$from)
I'd been told I don't need an .ini file and just to do it in the script considering we can put this script in any or our servers since its going to be internal. Thoughts?
Thanks
riskmod
01-30-2008, 08:10 AM
It looks like I have to manually force the script to get the smtp to work but still having issues with the smtp server understanding my script but here it is (using Linux server btw):
<?php
authSendEmail('me@mydomain.com', 'test email', 'This is a test');
function authSendEmail($toaddress, $subject, $message)
{
$smtpServer = "mysmtp.com";
$port = "25";
$timeout = "30";
$username = "me@mydomain.com";
$password = "pop3pass";
$localhost = "mail.your_domain.com";
$newLine = "\r\n";
//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
//Email From
fputs($smtpConnect, "MAIL FROM: $email" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
//Email To
fputs($smtpConnect, "RCPT TO: $toaddress" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: <$toaddress>" . $newLine;
$headers .= "From: $bname <$email>" . $newLine;
fputs($smtpConnect, "To: $toaddress\nFrom: $email\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>
Is there something I'm missing? Appreciate your help on my php noobiness.
jasonahoule
01-30-2008, 08:30 AM
That seems a bit excessive. You shouldn't have to open a socket connection and do it the hard way. Something like this should work...
ini_set("SMTP", mysmtp.com);
ini_set("smtp_port", 25);
mail($to, $subj, $msg, $headers);
riskmod
01-30-2008, 08:57 AM
thanks.
Only thing doing that gives me a:
SMTP server response: 550 Delivery is not allowed to this address on line 13.
Line 13: mail($to, $subj, $msg, $headers);
The defaults are:
$to = "me@domain.com";
$subj = "Test mail";
$msg = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
riskmod
01-30-2008, 12:20 PM
Jason I got it!!
<?php
$to = "you@domain.com";
$subj = "Test mail";
$msg = " think I got the PHP working!";
$from = "From: me@domain.com";
$headers = "From: $from";
ini_set('SMTP', 'smtp.com');
ini_set("smtp_port", 25);
mail($to, $subj, $msg, $from, $headers);
echo "Mail Sent.";
?>
Wahooooo! thanks for your help!!!
jasonahoule
01-30-2008, 01:01 PM
Glad you got it working!
Actual Soft
04-09-2009, 10:22 PM
It works fine in gmail and other domain email ids, but in yahoo it get in to the spam box. can this be avoided.
Please help me.. Thanks in advance
NogDog
04-09-2009, 11:44 PM
Getting past spam filters is a black art not completely understood by anyone. :rolleyes:
You can do google searches for something like "email headers and spam filters" to try and find out if there are any changes/additions you should make to your headers; but all of that may be to no avail if the host from which you are emailing is on their "blacklist" for some reason. (Telling the users to be sure to add your "From:" email address to their address books on those mail systems may help.)