www.webdeveloper.com
Results 1 to 10 of 10

Thread: manually putting smtp on php script..how?

  1. #1
    Join Date
    Oct 2006
    Posts
    203

    manually putting smtp on php script..how?

    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 Code:

    <?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

  2. #2
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    You can use the ini_set() function.

  3. #3
    Join Date
    Oct 2006
    Posts
    203
    So just as long as you do it like this?:

    PHP Code:
    <?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
    Last edited by riskmod; 01-29-2008 at 11:58 AM.

  4. #4
    Join Date
    Oct 2006
    Posts
    203
    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 Code:
    <?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($smtpConnect515);
        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($smtpConnect515);
        
    $logArray['authrequest'] = "$smtpResponse";
        
        
    //Send username
        
    fputs($smtpConnectbase64_encode($username) . $newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $logArray['authusername'] = "$smtpResponse";
        
        
    //Send password
        
    fputs($smtpConnectbase64_encode($password) . $newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $logArray['authpassword'] = "$smtpResponse";

        
    //Say Hello to SMTP
        
    fputs($smtpConnect"HELO $localhost$newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $logArray['heloresponse'] = "$smtpResponse";
        
        
    //Email From
        
    fputs($smtpConnect"MAIL FROM: $email$newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $logArray['mailfromresponse'] = "$smtpResponse";
            
        
    //Email To
        
    fputs($smtpConnect"RCPT TO: $toaddress$newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $logArray['mailtoresponse'] = "$smtpResponse";
        
        
    //The Email
        
    fputs($smtpConnect"DATA" $newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $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($smtpConnect515);
        
    $logArray['data2response'] = "$smtpResponse";
        
        
    // Say Bye to SMTP
        
    fputs($smtpConnect,"QUIT" $newLine);
        
    $smtpResponse fgets($smtpConnect515);
        
    $logArray['quitresponse'] = "$smtpResponse";    
    }
    ?>
    Is there something I'm missing? Appreciate your help on my php noobiness.

  5. #5
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    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...
    PHP Code:
    ini_set("SMTP"mysmtp.com);
    ini_set("smtp_port"25);

    mail($to$subj$msg$headers); 

  6. #6
    Join Date
    Oct 2006
    Posts
    203
    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";
    Last edited by riskmod; 01-30-2008 at 09:25 AM.

  7. #7
    Join Date
    Oct 2006
    Posts
    203

    Igotit

    Jason I got it!!

    PHP Code:
    <?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!!!

  8. #8
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    Glad you got it working!

  9. #9
    Join Date
    Apr 2009
    Posts
    1

    Thanks for the script

    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

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,400
    Getting past spam filters is a black art not completely understood by anyone.

    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.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles