简体   繁体   中英

Send mail via SMTP?

I have a form on my website where people can join events. The code behind it works in this way:

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

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

The issue is that these emails are sent unauthenticated through a default email on the hosting account. I had 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?

<?
// 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/send.php" );} 
else 
{print "MISSING EMAIL ADDRESS ALL FILDS MUST BE FILLED!"; } 
}
}
?>

Best if you use a dedicated script for sending emails. Such as PHPMailer which gives you various config options including SMTP:

// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP

// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "email@email.com";
$mail->Password = "password";            
$mail->Port = 465; // change port is required

This is one way to do it. You need to include PHPMailer

#somewhere in code before you call function
include($path_to_PHPMailer."class.smtp.php");

function sendEmail($email,$name,$title,$content,$who=false,$att=array('')){
    //VARIABLE $email is email of sender 
    //VARIABLE $name is name of sender
    //VARIABLE $title is title of email
    //VARIABLE $content is content of email
    $mail = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    // DOUBLE $mail->Host       = "your email_server"; // SMTP server 
    $mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "your email_server"; // sets the SMTP server
    $mail->Port       = server port;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "your username"; // SMTP account username
    $mail->Password   = "your password";        // SMTP account password

    if($who){
        $mail->SetFrom($email, $name);
        $mail->AddReplyTo($email,$name);
        $address = "set your email";//EMAIL OF RECIPENT
    } else{
        $mail->SetFrom("set your email", "your name (or service name)");
        $mail->AddReplyTo("set your email","your name (or service name)");
        $address = $email;
    }

    $content=str_replace("\n", "<br>", $content);
    $mail->Subject    = $title;
    $mail->MsgHTML($content);
    $mail->AddAddress($address, $name);
    $mail->CharSet='utf-8';

    if($att[0]!=''){
        foreach ($att as $at){
            $mail->AddAttachment($at);
        }
    }

    if(!$mail->Send()) {
        return false; //$mail->ErrorInfo;
    } else {
        return true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM