在我的网站上发送SMTP邮件

时间:2013-06-30 22:59:58

标签: php mysql email smtp

我尝试将我的php邮件脚本设置为SMTP邮件脚本,因为我的托管服务提供商不允许在没有验证的情况下发送邮件。
我正在尝试从我的网站发送电子邮件到我的Gmail帐户。
我必须发送SMTP邮件,但我无法将该脚本转换为SMTP邮件表单 以下是代码:

   <?php

$websitename="My site name"; 

$allowtypes=array("jpg", "png", "gif");
$myemail="muratcol@gmail.com"; // THIS IS THE MAIL ADDRESS THAT I TRY TO SEND
$priority="2"; 
$allowattach="1"; 
$max_file_size="1500";
$max_file_total="3000";
$submitvalue="Send"; 
$resetvalue="Reset";
$defaultsubject="No subject"; 
$use_subject_drop=false;
$subjects=array("Department 1", "Department 2", "Department 3");
$emails=array("dept_1@domain.com", "dept_2@domain.com", "dept_3@domain.com");
$thanksmessage="Thank you"; 
function get_ext($key) { 
    $key=strtolower(substr(strrchr($key, "."), 1));
    $key=str_replace("jpeg", "jpg", $key);
    return $key;
}

function phattach($file, $name, $boundary) {

    $fp=fopen($file, "r");
    $str=fread($fp, filesize($file));
    $str=chunk_split(base64_encode($str));
    $message="--".$boundary."\n";
    $message.="Content-Type: application/octet-stream; name=\"".$name."\"\n";
    $message.="Content-disposition: attachment; filename=\"".$name."\"\n"; 
    $message.="Content-Transfer-Encoding: base64\n";
    $message.="\n";
    $message.="$str\n";
    $message.="\n";

    return $message;
}

function clean_msg($key) {
    $key=str_replace("\r", "", $key);
    $key=str_replace("\n", "", $key);
    $find=array(
        "/bcc\:/i",
        "/Content\-Type\:/i",
        "/Mime\-Type\:/i",
        "/cc\:/i",
        "/to\:/i"
    );
  $key=preg_replace($find, "", $key);
  return $key;
}

$error="";
$sent_mail=false;

If($_POST['submit']==true) {
    extract($_POST, EXTR_SKIP);

        If(trim($yourname)=="") { 
            $error.="İsminizi giriniz!<br />";
        }

        If(trim($youremail)=="") { 
            $error.="E-Posta adresinizi giriniz!<br />";
        } Elseif(!preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/", $youremail)) {
            $error.="Wrong mail address.<br />";
        }

        If(trim($emailsubject)=="") {
            $emailsubject=$defaultsubject;
        }

        If(trim($yourmessage)=="") { 
            $error.="You didnt enter your message!<br />";
        }

        If($allowattach > 0) {

            If((array_sum($_FILES['attachment']['size'])) > ($max_file_total*1024)) {

                $error.="Max".$max_file_total."kb<br />";

            } Else {

                For($i=0; $i <= $allowattach-1; $i++) {

                    If($_FILES['attachment']['name'][$i]) {

                        If(!in_array(get_ext($_FILES['attachment']['name'][$i]), $allowtypes)) {

                            $error.= "Unvalid extension: ".$_FILES['attachment']['name'][$i]."<br />";

                        } Elseif(($_FILES['attachment']['size'][$i]) > ($max_file_size*1024)) {

                            $error.= "Picture so high : ".$_FILES['attachment']['name'][$i].";

                        } 

                    } 

                } 

            } 

        } 

    If($error) {

        $display_message=$error;

    } Else {

        If($use_subject_drop AND is_array($subjects) AND is_array($emails)) {
            $subject_count=count($subjects);
            $email_count=count($emails);

            If($subject_count==$email_count) {

                $myemail=$emails[$emailsubject];
                $emailsubject=$subjects[$emailsubject];

            } // If $subject_count

        } // If $use_subject_drop

        $boundary=md5(uniqid(time()));

        $headers="Return-Path: <".clean_msg($youremail).">\n";
        $headers.="From: ".clean_msg($yourname)." <".clean_msg($youremail).">\n";
        $headers.="X-Mailer: PHP/".phpversion()."\n";
        $headers.="X-Sender: ".$_SERVER['REMOTE_ADDR']."\n";
        $headers.="X-Priority: ".$priority."\n"; 
        $headers.="MIME-Version: 1.0\n";
        $headers.="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
        $headers.="This is a multi-part message in MIME format.\n";

        $message = "--".$boundary."\n";
        $message.="Content-Type: text/html; charset=\"utf-8\"\n";
        $message.="Content-Transfer-Encoding: quoted-printable\n";
        $message.="\n";
        $message.=clean_msg(nl2br(strip_tags($yourmessage)));
        $message.="\n";

        If($allowattach > 0) {

            For($i=0; $i <= $allowattach-1; $i++) {

                If($_FILES['attachment']['tmp_name'][$i]) {

                    $message.=phattach($_FILES['attachment']['tmp_name'][$i], $_FILES['attachment']['name'][$i], $boundary);

                } 

            } 

        } 
    $message.="--".$boundary."--\n";

    $adjunto_name=$_FILES['attachment']['name']; 
    $adjunto_type=$_FILES['attachment']['type']; 
    $adjunto_size=$_FILES['attachment']['size']; 
    $adjunto_temp=$_FILES['attachment']['tmp_name']; 

    if(is_uploaded_file($_FILES['attachment']['tmp_name'][0]) && is_uploaded_file($_FILES['attachment']['tmp_name'][1])){


        If(!mail($myemail, clean_msg($emailsubject), $message, $headers)) {

            Exit("Problem !!!\n");

        } Else {

            $sent_mail=true;

        }
}else{

    $error.="Please upload photo.";
    $display_message=$error;
}

} 

} 
?>

0 个答案:

没有答案