SwiftMailer: TLS for SMTP Connection Tyle
Posted: Thu Nov 27, 2008 4:43 pm
How can I get my script to connect using "TLS" for the SMTP connection type instead of "OPEN"?
Code: Select all
<?php
require_once 'Swift/Connection/SMTP.php';
require_once 'Swift/Authenticator/LOGIN.php';
require_once 'Swift.php';
if ( isset( $_POST[ 'f2' ] ) ) {
$f2 = $_POST[ 'f2' ];
if ( isset( $f2['smtp_user'] ) && !empty( $f2['smtp_user'] ) ) {
} else {
$f2['smtp_server'] = '';
}
} else {
$f2['smtp_server'] = 'smtp.gmail.com';
$f2['smtp_port'] = '465';
$f2['smtp_user'] = 'example@gmail.com';
$f2['smtp_password'] = 'password';
}
if ( isset( $_POST[ 'cmd2' ] ) ) {
$cmd2 = $_POST[ 'cmd2' ];
} else {
$cmd2 = '';
}
if ( $cmd2 == 'check2' ) {
$smtp_conn = & new Swift_Connection_SMTP( $f2['smtp_server']);
$smtp_conn->smtp_type = 'tls';
if ( isset( $f2['smtp_user'] ) && !empty( $f2['smtp_user'] ) ) {
$smtp_conn->setUsername( $f2['smtp_user'] );
}
if ( isset( $f2['smtp_password'] ) && !empty( $f2['smtp_password'] ) ) {
$smtp_conn->setPassword( $f2['smtp_password'] );
}
if ( isset( $f2['smtp_port'] ) && !empty( $f2['smtp_port'] ) ) {
$smtp_conn->setPort( $f2['smtp_port'] );
}
try {
if($swift =& new Swift( $smtp_conn )) echo '<h1>Pass</h1>';
//echo '<pre>'; var_dump( $swift );echo '</pre>';
}
catch(Swift_ConnectionException $e) {
echo "<h1>Fail</h1>";
}
}
?>
<div>
<form name="categories" action="./" method="post">
<input type="hidden" name="cmd2" value="check2"/>
<table cellspacing="1" cellpadding="2" border="0" width="450" class="admin_table" summary="settings">
<tr>
<td class="text_normal_9pt_left">SMTP Server:</td>
<td class="text_normal_9pt_left"><input type="text" name="f2[smtp_server]" value="<?php if (!empty($f2['smtp_server'])) { echo( $f2['smtp_server'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
<td class="text_normal_9pt_left">SMTP Server Port:</td>
<td class="text_normal_9pt_left"><input type="text" name="f2[smtp_port]" value="<?php if (!empty($f2['smtp_port'])) { echo( $f2['smtp_port'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
<td class="text_normal_9pt_left">SMTP Username:</td>
<td class="text_normal_9pt_left"><input type="text" name="f2[smtp_user]" value="<?php if (!empty($f2['smtp_user'])) { echo( $f2['smtp_user'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
<td class="text_normal_9pt_left">SMTP Password:</td>
<td class="text_normal_9pt_left"><input type="password" name="f2[smtp_password]" value="<?php if (!empty($f2['smtp_password'])) { echo( $f2['smtp_password'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
<td class="text_normal_9pt_left"></td>
<td class="text_normal_9pt_left"><input type="submit" name="check2" value="Check" /></td>
</tr>
</table>
</form>
</div>