PHPmail遇到的问题

摘要:总结一下碰到的问题

1、无法发送附件

附件不能使用url需要使用绝对路径
地址不能用\需要用/

2、from和subject中文乱码,但是内容是正常的

是应为在base64转码的时候发生错误,需要处理
$from_name = $_POST['FromName'];
$mail->FromName = "=?utf-8?B?".base64_encode("$from_name")."?=";

3、调试模式

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;

4、无法用其他的邮箱发送,只能使用gmail

换个稳定的phpmail包
检查host信息
from  setfrom  fromname 这些设置分不清可参考下面的
<?php
/**
 * UserName, Password,  FromAddress,  FromName,  ToAddress,  ToName,  Subject, Body, IsHtml, AltBody,CCAddress
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

//require 'PHPMailer/PHPMailerAutoload.php';
require '../ThinkPHP/Library/Vendor/PHPMailer/class.phpmailer.php';
require '../ThinkPHP/Library/Vendor/PHPMailer/class.pop3.php';
require '../ThinkPHP/Library/Vendor/PHPMailer/class.smtp.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
$api_get = $_GET;
$api_post = $_POST;
$api_data = array_merge($api_get,$api_post);
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
//$mail->Host = 'smtp.gmail.com';
//$mail->Host = 'email-smtp.us-west-2.amazonaws.com';
//$mail->Host = 'smtp.qq.com';
$mail->Host = $api_data['host'];
$mail->Charset='UTF-8';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $api_data['UserName'];
//Password to use for SMTP authentication
$mail->Password = $api_data['Password'];//"ehtjdrgvldqjpkoe";
//Set who the message is to be sent from
$from_name = $api_data['FromName'];
$from_name = "=?utf-8?B?".base64_encode("$from_name")."?=";
$mail->setFrom($api_data['FromAddress'], $from_name);
//$mail->FromName = "=?utf-8?B?".base64_encode("$from_name")."?=";
//$mail->setFrom("=?utf-8?B?".base64_encode("$from_name")."?=", $api_data['FromName']);
//echo $from_name;
//echo 'from name ok';
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$to_arr = array_unique(explode(';',$api_data['ToAddress']));
foreach ($to_arr as $key1 => $value1){
    $mail->addAddress($value1, $api_data['ToName']);
}
if(!empty($api_data['CCAddress'])){
    $CCAddress = explode(';', $api_data['CCAddress']);
    for($CCi =0;$CCi < count($CCAddress);$CCi++)
    {
        $mail->addCC($CCAddress[$CCi]);
    }
}
if(!empty($api_data['BCCAddress'])){
    $BCCAddress = explode(';', $api_data['BCCAddress']);
    for($BCCi = 0; $BCCi < count($BCCAddress);$BCCi++)
    {
        $mail->addBCC($BCCAddress[$BCCi]);
    }
}

//Set the subject line
if((strtolower($api_data['IsHtml']) == 'true') || ($api_data['IsHtml'] == true))
{
    $mail->isHTML();
    if(!empty($api_data['AltBody'])){
        $mail->AltBody = $api_data['AltBody'];
    }
}
$use_subject = $api_data['Subject'];
//echo $use_subject;
//echo 'subject ok';
$mail->Subject = "=?utf-8?B?".base64_encode("$use_subject")."?=";
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body=htmlspecialchars_decode($api_data['Body']);

if(!empty($_FILES['FileData0']['tmp_name'])){
    //Attach multiple files one by one
    for ($ct = 0; $ct < 10; $ct++)
    {
        $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['FileData'.$ct]['name']));
        if (move_uploaded_file($_FILES['FileData'.$ct]['tmp_name'], $uploadfile))
        {
            $mail->addAttachment($uploadfile, $_FILES['FileData'.$ct]['name']);
        }
    }
}

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent success!";
}

Gmail 发送报错530

打开gmail 中的 安全性较低的访问权限
OK

Gmail腾讯阿里等可以正常,网易邮箱出现乱码

1、查看邮件 在更多里面选择编码 选择utf8发现可以正常显示
2、此时将phpmail中的后续设置的编码设置为utf8同时将包的编码也设置为utf8否则不可以
class.phpmailer.php 中 public $CharSet           = 'UTF-8';  原始为iso-8859-1
具体调用里面 $mail->Charset='UTF-8';
评论
  • 2018-12-12 14:24:48 by Eric Guo
    587这个端口好像是通用的,测试后qq gmail aws都是支持的