SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    21
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Send mail using PHP script via SMTP

    I have a form on my website where people can join events. The codes behind he for works this way:

    1.
    All info is saved in a database. This part work fine

    2.
    The second part of the codes send out an email to me and to the user with the info he intered (same info as saved in the database)

    The issue is that the emails are sent unauthenticated through a default email on the hosting account. I have to modify the script to force SMTP authentication with a valid email under my hosting account to fix the error. Right now the script sends out the email but it ends in spamfilter with all ISPs so the user never receive the email.

    I have no idea of how to do, or create the codes so the script use SMTP authentication. Below is the codes I have so fare. Can someone help me?

    PHP Code:
    <?
    // SEND OUT EMAIL PART
    // COPY SEND TO MY SELF
     
    $to "my@email.com"
     
    $from $_REQUEST['email'] ; 
     
    $name $_REQUEST['name'] ; 
     
    $headers "From: $from"
     
    $subject "Thanks!"
     
     
    $fields = array(); 
     
    $fields{"name"} = "Name"
     
    $fields{"address"} = "Address"
     
    $fields{"phone"} = "Phone"
     
    $fields{"email"} = "E-mail addesse"
     
     
    $body "INFO:\n\n"; foreach($fields as $a => $b){     $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
     
     
    // SEND TO THE USER
     
    $headers2 "From: my@email.com"
     
    $subject2 "THANKS!"
     
     
    $fields2 = array(); 
     
    $from $_REQUEST['email'] ; 
     
    $name $_REQUEST['name'] ; 
     
    $headers "From: $from"
     
    $subject "Thanks!"

     
    $body2 "
     
    TEXT TO EMAIL RECEIVER

    \n\n"
    ; foreach ($fields2 as $a => $b){     $body2 .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

     
    // ERROR MESSAGES
     
    if($from == '') {print "MISSING EMAIL ADDRESS.";} 
     else { 
     if(
    $name == '') {print "MISSING NAME";} 
     else { 
     
    $send mail($to$subject$body$headers); 
     
    $send2 mail($from$subject2$body2$headers2); 
     if(
    $send
     {
    header"Location: http://mysite.com/send.php" );} 
     else 
     {print 
    "MISSING EMAIL ADDRESS ALL FILDS MUST BE FILLED!"; } 
     }
    }
     
    ?>

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,409
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    This is how I setup an smtp email:

    PHP Code:
    $message "Your message including any html";

    // Include the PEAR classes
    include('Mail.php');
    include(
    'Mail/mime.php');

    // Constructing the email
    $sender "me@domain.com";
    $recipient "somebody@home.com";
    $subject "Information requested";
    // smtp mail server - get from your hosts
    $host "mail.server.com";
    $username "me@domain.com";
    // Sender email address password
    $password "123456";
    $crlf "\n";
    $headers = array(
        
    'From'          => $sender,
        
    'Return-Path'   => $sender,
        
    'Subject'       => $subject
        
    );

    // Creating the Mime message
    $mime = new Mail_mime($crlf);

    // Setting the body of the email
    $mime->setTXTBody($text);
    $mime->setHTMLBody($message);
    $body $mime->get();
    $headers $mime->headers($headers);

    // Setup the SMTP part
    $smtp Mail::factory('smtp',
       array (
    'host' => $host,
        
    'auth' => true,
        
    'username' => $username,
        
    'password' => $password));

    // Send the email via STMP    
    $mail $smtp->send($recipient$headers$body);



Tags for this Thread

Bookmarks

Posting Permissions

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