通过SMTP发送邮件?

时间:2014-02-08 13:47:28

标签: php email smtp send

我的网站上有一个人们可以加入活动的表格。他背后的代码是这样的:

1。 所有信息都保存在数据库中。这部分工作正常

2。 代码的第二部分向我和用户发送一封电子邮件,其中包含他所信息的信息(与数据库中保存的信息相同)

问题是电子邮件是通过托管帐户上的默认电子邮件未经身份验证发送的。我必须修改脚本以使用我的主机帐户下的有效电子邮件强制SMTP身份验证来修复错误。现在,脚本会发送电子邮件,但它会在所有ISP的垃圾邮件过滤器中结束,因此用户永远不会收到电子邮件。

我不知道如何操作或创建代码,因此脚本使用SMTP身份验证。以下是我所拥有的代码。有人能帮助我吗?

<?
// 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!"; } 
}
}
?>

2 个答案:

答案 0 :(得分:0)

最好使用专用脚本发送电子邮件。例如PHPMailer为您提供各种配置选项,包括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

答案 1 :(得分:0)

这是一种方法。您需要添加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;
    }
}