iT邦幫忙

0

GMAIL SMTP 發信

各位大神們要請問

我想設定用 GMAIL SMTP 做聯絡我們

但不知道為什麼出錯了

錯誤訊息如下

SMTP Error: Could not connect to SMTP host. 發送錯誤: SMTP Error: Could not connect to SMTP host.

我自已寫的語法如下


<?php
include("class.phpmailer.php"); //匯入PHPMailer類別

$Name=$_POST['sndname'];
$Mail=$_POST['sendmail'];
$Subject=$_POST['subject'];
$Sendbody=$_POST['sendbody'];

$mail= new PHPMailer(); //建立新物件
$mail->IsSMTP(); //設定使用SMTP方式寄信
$mail->SMTPAuth = true; //設定SMTP需要驗證
$mail->SMTPSecure = "ssl"; // Gmail的SMTP主機需要使用SSL連線
$mail->Host = "smtp.gmail.com"; //Gamil的SMTP主機
$mail->Port = 465;  //Gamil的SMTP主機的埠號(Gmail為465)。
$mail->CharSet = "utf-8"; //郵件編碼

$mail->Username = "xxx@gmail.com"; //Gamil帳號
$mail->Password = "xxxxxxxxxxxxxxxxxxxx"; //Gmail密碼

$mail->From = $Mail; //寄件者信箱
$mail->FromName = "線上客服"; //寄件者姓名

$mail->Subject ="一封線上客服信";  //郵件標題
$mail->Body = "姓名:".$Name."<br>信箱:".$Mail."<br>主題:".$Subject."<br>回應內容:".$Sendbody; //郵件內容

$mail->IsHTML(true); //郵件內容為html ( true || false)
$mail->AddAddress("xxx@gmail.com"); //收件者郵件及名稱

if(!$mail->Send()) {
	echo "發送錯誤: " . $mail->ErrorInfo;
} else {
	echo "<div align=center>感謝您的回覆,我們將會盡速處理!</div>";
}
?>
https://ithelp.ithome.com.tw/articles/10190109 => 之前的回答
你少了一個步驟
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
小魚
iT邦大師 1 級 ‧ 2018-08-28 12:24:30

你gmail有設定低安全性嗎?

看更多先前的回應...收起先前的回應...
mack88888 iT邦新手 5 級 ‧ 2018-08-28 12:27:28 檢舉

有 ! 以設定

mack88888 iT邦新手 5 級 ‧ 2018-08-28 12:35:47 檢舉
小魚 iT邦大師 1 級 ‧ 2018-08-28 12:38:04 檢舉

沒用PHP寄過, 不過我用C#寄信是用 25 Port, 不知道會不會有關係?
我好像沒設定 SMTPAuth ?

小魚 iT邦大師 1 級 ‧ 2018-08-29 07:38:51 檢舉

我原本也得到跟你一樣的訊息,後來把Port改成25就可以發信了,不過會有亂碼的問題。

0
code
iT邦新手 5 級 ‧ 2018-08-28 16:21:30
public class JMail {
    public static Tool jtool = javaapplication4.JavaApplication4.jtool;
    static String host = "smtp.gmail.com";
    static int port = 587;
    static final String username = Gmail_user;
    static final String password = Gmail_password;

    public static void Send_mail(String To_mail, String Mail_title, String Mail_text) {
        
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", port);
        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To_mail));
            message.setSubject(Mail_title);
            message.setText(Mail_text);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, port, username, password);
            Transport.send(message);
            System.out.println(jtool.getDateTime(1) + " " + To_mail + "  " + Mail_title + " " + "寄送email結束.");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

我是用587,JAVA寫的,請參考

我要發表回答

立即登入回答