PHP邮件脚本不再有效,需要添加SMTP

时间:2015-07-20 18:10:36

标签: php email

我在Hostgator上托管我的服务器,当我们选择订阅我的表格时,我会通过邮件向我的订户发送折扣和优惠券。不幸的是,某些电子邮件提供商不再收到我的邮件,经过一些研究后,我意识到我应该使用我的邮件脚本进行SMTP身份验证。我使用了一个默认的邮件脚本,我正在查看一个PHPmailer示例,但我很困惑如何使用$mail->Send();(见此处:enter link description here)的PHP邮件调整我的发送。我使用$sendit =@mail($email_to, $email_subject),我必须做些什么来使消息主题/正文适应新脚本?

<?
// Use PDO to connect to the DB
$dsn = '*****';
$user = '*****';
$password = '*****';

try {
$db = new PDO($dsn, $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} 
catch (PDOException $e) {
die_with_error('PDO Connection failed: ' . $e->getMessage());
}
function die_with_error($error) {
$db = null;
header('Location: error.php?msg=' . $error);
}

function die_with_success($email, $debug) {
$db = null;
//header('Location: success.php?email=' . $email . '&debug=' . $debug);
header('Location: success.php?email=' . $email);
}

function send_email($coupon, $coupon_url){
// Email From
$email_from = "coupons@default.com";

// Email To
$email_to = $coupon->email;

// Email Subject
$email_subject = "The Coupon You Requested";

//Add UTM string to Coupon URL
$coupon_url = $coupon_url . $coupon->utmString;

// Email Message
// This is an HTML formatted message
$email_html_msg = file_get_contents('email_templates/coupons.html', true);
$email_html_msg = str_replace("*|COUPON-PDF|*", $coupon_url, $email_html_msg);
$email_html_msg = str_replace("*|COUPON-IMG|*", $coupon->smallImg, $email_html_msg);
$email_html_msg = str_replace("*|EMAIL|*", $coupon->email, $email_html_msg);
$email_html_msg = str_replace("*|FIRSTNAME|*", $coupon->first_name, $email_html_msg);
$email_html_msg = str_replace("*|LASTNAME|*", $coupon->last_name, $email_html_msg);
$email_html_msg = str_replace("*|ZIPCODE|*", $coupon->zip, $email_html_msg);
// This is a plain text formatted message
$email_plain_msg = "Click the link below to download your coupon:\r\n";
$email_plain_msg .= $coupon_url;

// start setting up the email header
$headers = "From: ".$email_from;

// create boundary string
// boundary string must be unique using MD5 to generate a pseudo random hash
$random_hash = md5(date('r', time())); 
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x";

// set email header as a multipart/mixed message
// this allows the sending of an attachment combined with the HTML message
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// multipart boundary for the HTML message
$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=UTF-8\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_html_msg . "\n\n";

// multipart boundary for the plain text message
$email_message .= "--{$mime_boundary}\n" .
"Content-Type:text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_plain_msg . "\n\n";


// end the multipart message
$email_message .= "--{$mime_boundary}--\n";


require("class.phpmailer.php");
$mail = new PHPMailer();


// try to send the email and verify the results
$sendit = @mail($email_to, $email_subject, $email_message, $headers);
if(!$sendit) {
  die_with_error("The Email could not be sent.");
}
}

1 个答案:

答案 0 :(得分:1)

我使用主机鳄鱼。

这是我使用的一组简单函数。

 /**
  * Summary of gfEmail
  * @param mixed $_Addrees - the email address to send to
  * @param mixed $_Message - the eemail body
  * @param mixed $_Subject - The email subject
  */
 function gfEmail($_Address, $_Message, $_Subject)
 {

     $Mail->addAddress($Address);
     $Mail->Subject=$_Subject;
     $Mail->Body=$_Message;
     gfSendIt($Mail);
 }

 /**
  * Summary of gfSendIt
  * @param object $_Mail
  */
 function gfSendIt($_Mail)
 {

     //SMTP needs accurate times, and the PHP time zone MUST be set
     date_default_timezone_set('Etc/UTC');
     //Create a new PHPMailer instance
     //$mail = new PHPMailer;
     //Tell PHPMailer to use SMTP
     $Mail->IsHTML(true);
     $Mail->isSMTP();
     //Enable SMTP debugging
     // 0 = off (for production use)
     // 1 = client messages
     // 2 = client and server messages
     $Mail->SMTPDebug=3;
     $Mail->Debugoutput='html';
     //Set the hostname of the mail server
     $Mail->Host="mail.yourdomainname.com";
     //Set the SMTP port number - likely to be 25, 465 or 587
     $Mail->Port=26;
     $Mail->SMTPAuth=true;
     //$Mail->SMTPSecure='tls';
     $Mail->Username=MAILUSERNAME;
     $Mail->Password=MAILPASSWORD;
     $Mail->AuthType="LOGIN";
     $Mail->setFrom('sent from email address');

     //send the message, check for errors
     if(!$Mail->send()) {
         echo "Mailer Error: ".$Mail->ErrorInfo;
     }
     else {
         //echo "Message sent!";
     }
 }

所以要发送电子邮件:

$Body = 'This is the email body.';
gfEmail('someemail@address.com', $Body, 'Email subject');