[英]Send mail via SMTP?
我的網站上有一個表格,人們可以在其中加入活動。 它背后的代碼是這樣工作的:
所有信息都保存在數據庫中。 這部分工作正常。
代碼的第二部分向我和用戶發送一封電子郵件,其中包含他輸入的信息(與保存在數據庫中的信息相同)
問題是這些電子郵件是通過托管帳戶上的默認電子郵件未經身份驗證發送的。 我不得不修改腳本以強制使用我的托管帳戶下的有效電子郵件進行 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!"; }
}
}
?>
最好使用專用腳本發送電子郵件。 例如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
這是一種方法。 您需要包含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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.