SwiftMailer: TLS for SMTP Connection Tyle

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
hknight
Forum Newbie
Posts: 13
Joined: Wed Aug 13, 2008 2:48 pm

SwiftMailer: TLS for SMTP Connection Tyle

Post by hknight »

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>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: SwiftMailer: TLS for SMTP Connection Tyle

Post by John Cartwright »

You know.. its polite and helpful to the community to let us know things wen't well in your other thread. The only reason I point that out is you took my advise in the other thread without posting that it worked ok (so future readers may benefit as well).

To answer your question, it's the 3rd parameter in the connection object.

Code: Select all

new Swift_SMTP_Connection('smtp.gmail.com', 587, SWIFT_TLS)
hknight
Forum Newbie
Posts: 13
Joined: Wed Aug 13, 2008 2:48 pm

Re: SwiftMailer: TLS for SMTP Connection Tyle

Post by hknight »

Thanks
Post Reply