如何在PHP中使用Swift邮件程序?

时间:2017-03-19 19:28:15

标签: php

我在php中使用swift邮件程序时遇到错误。 错误是tls端口号错误,没有连接。 我的tls端口是否正确请在此代码中提供帮助。     require_once'Swift-5.0.1 / lib / swift_required.php';

// Grab the Data
$emailc=$emailid;
$name=filter_var(''.$name1.'',FILTER_SANITIZE_STRING);
$uname1=filter_var(''.$uname.'',FILTER_SANITIZE_STRING);
$psw1=filter_var(''.$psw.'',FILTER_SANITIZE_STRING);
$email=filter_var($emailc,FILTER_SANITIZE_EMAIL);

// Create our body messag
$data = "Nanme:".$name."<br>Your username is <b>".$uname1."</b> and password is <b>".$psw1."</b> ";

//Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',443,'tls')
                                    ->setUsername('xyz@gmail.com')
                                    ->setPassword('xyz121212');


// Create the mailer
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Demo')
                    ->setFrom (array('demo@gmail.com' => 'Demo'))
                    ->setTo (array(''.$email.'' => 'Add Recipients'))
                    ->setSubject ('Thanks, This mail is for remind your appoinment')
                    ->setBody ($data, 'text/html');

// Send the message
$result = $mailer->send($message);

1 个答案:

答案 0 :(得分:1)

你的代码是正确的只是tls端口是错的tls端口是587 港口 tls = 587 ssl = 443 所以只需将443更改为587。

// Grab the Data
$emailc=$emailid;
 $name=filter_var(''.$name1.'',FILTER_SANITIZE_STRING);
  $uname1=filter_var(''.$uname.'',FILTER_SANITIZE_STRING);
   $psw1=filter_var(''.$psw.'',FILTER_SANITIZE_STRING);
 $email=filter_var($emailc,FILTER_SANITIZE_EMAIL);


// Create our body messag
 $data = "Nanme:".$name."<br>Your username is <b>".$uname1."</b> and password is <b>".$psw1."</b> ";


//Create the transport
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com',587,'tls')
    ->setUsername('xyz@gmail.com')
    ->setPassword('xyz121212');


// Create the mailer
 $mailer = Swift_Mailer::newInstance($transport);
 $message = Swift_Message::newInstance('Demo')
  ->setFrom (array('demo@gmail.com' => 'Demo'))
  ->setTo (array(''.$email.'' => 'Add Recipients'))
  ->setSubject ('Thanks, This mail is for remind your appoinment')
  ->setBody ($data, 'text/html');

// Send the message
 $result = $mailer->send($message);