[SOLVED]problem in using swiftmailer with google smtp server

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

rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

[SOLVED]problem in using swiftmailer with google smtp server

Post by rekha_harnoor »

Hi All,

I am using php5 and Swift-3.1.2-php5.zip windows version. I have unzipped and copied to c:/ directory.


I want to send a simple mail. My code is below and I am getting a blank page.
I have created google account and using that smtp.gmail.com. please let me know what to do next.
I have tried phpmailer etc and nothing is working.

Code: Select all

<?php

require_once "c:/Swiftphp5/lib/Swift.php";
require_once "c:/Swiftphp5/lib/Swift/Connection/SMTP.php";
 
//Start Swift
$swift =& new Swift(new Swift_Connection_SMTP("smtp.gmail.com"));
 
//Create the message
$message =& new Swift_Message("My subject", "My body");
 
//Now check if Swift actually sends it
if ($swift->send("rekha_harnoor@yahoo.com", "rekhakulka@gmail.com","The Subject", "The body")) echo "Sent";
else echo "Failed";
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Gmail uses TLS encryption. Firstly you'll need PHP compiled with OpenSSL, then you'll need to change your connection line:

There are examples on the very first code example for the SMTP docs:

http://www.swiftmailer.org/wikidocs/v3/connections/smtp

You also need a Gmail username and password:

PS: It looks like you're trying to use version 2 code with version 3. That won't work. Have a read over the wiki:

http://www.swiftmailer.org/wikidocs/
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

Post by rekha_harnoor »

I didnot understand the following. What exactly I supposed do?



Firstly you'll need PHP compiled with OpenSSL, then you'll need to change your connection line:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just try connecting the way it shows in the documentation:

Code: Select all

//Connect to Gmail (PHP5)
$swift = new Swift(new Swift_Connection_SMTP(
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
You'll soon find out if PHP doesn't have support for TLS since it will throw an error in that case. PHP is open-source, so whoever administers the server (or the package the server uses) compiles it from source code with certain configure flags. They need to have configured PHP "--with-openssl". You probably don't even need to worry about it if you just update your code as per my examples in the docs.
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

Post by rekha_harnoor »

Hi,

Thanks for your reply. But still the problem is not solved.
I modified my code. But still I am just getting blank page.

Am I missing something? Do I need to do anything in php.ini ?

Code: Select all

<?php 

require_once "c:/Swiftphp5/lib/Swift.php"; 
require_once "c:/Swiftphp5/lib/Swift/Connection/SMTP.php"; 
  
//Connect to Gmail (PHP5) 
$swift = new Swift(new Swift_Connection_SMTP( 
    "smtp.gmail.com", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));
  
    
     
//Create the message 
$message =& new Swift_Message("My subject", "My body"); 
  
//Now check if Swift actually sends it 
if ($swift->send("rekha_harnoor@yahoo.com", "rekhakulka@gmail.com","The Subject", "The body")) echo "Sent"; 
else echo "Failed"; 
?>
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

Post by rekha_harnoor »

for your information I have already uncommented extension = php_openssl.dll in php.ini file
But still getting blank page.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

rekha_harnoor wrote:for your information I have already uncommented extension = php_openssl.dll in php.ini file
But still getting blank page.
Totally blank page?

That suggests there's a fatal error. Turn error reporting on and display_errors:

Code: Select all

<?php ini_set("display_errors", "On"); error_reporting(E_STRICT); ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unless something has changed, E_ALL | E_STRICT will be needed to have all errors.. ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:Unless something has changed, E_ALL | E_STRICT will be needed to have all errors.. ;)
I learn something new every day :) Thanks.
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

Post by rekha_harnoor »

included your code.
But still getting blank page.
No errors are displayed.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's probably a parse error then.
Run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

Post by rekha_harnoor »

The output is as follows:



PHP Version: 5.1.6
PHP OS: WINNT
Error Reporting: 2047 (E_ALL)
Register Globals: Off
Short Tags: Off
Display Errors: Off
Magic Quotes GPC: Off
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Config file: C:\php\php.ini
Loaded Extensions:
bcmath calendar com_dotnet ctype
date ftp hash iconv
odbc pcre Reflection session
libxml standard tokenizer zlib
SimpleXML dom SPL wddx
xml xmlreader xmlwriter apache2handler
gd mysql openssl xsl
mysqli
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You got a phpinfo() page?
rekha_harnoor
Forum Commoner
Posts: 32
Joined: Mon Feb 19, 2007 3:17 am

Post by rekha_harnoor »

no this is not a phpinfo page.

This is output of the code given by the fyed
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry, I meant, can you post one? I just want to have a look what streams you have registered (i.e. is TLS in there).

Code: Select all

<?php ini_set("display_errors", "On"); ?>
Just do that by itself at the top of all your code. My E_STRICT setting overrode your E_ALL and you have display_errors off.
Post Reply