12.3.09

Adding gmail SMTP to symfony 1.0

PHPMailer is already included in symfony, so why add one yourself?

The thing is, I never get PHPMailer that is bundled with symfony to work with my gmail account so I decided to find something that can be done.

1.) Create a file myMailer.class.php inside your lib directory (either in apps/app_name/lib or in your root lib directory)
2.) Put these codes in there:


class myMailer {

public function __construct($username, $password, $user_alias) {
$this->from = $username;
$this->password = $password;
$this->user_alias = $user_alias;
}

public function send($to, $subject, $body) {

$eol = "\r\n";
$host = 'gmail-smtp.l.google.com';
$port = 465; $fname = $this->user_alias;
$data = 'Date: ' . date ( 'r', time () ) . $eol;
$data .= 'From: "' . $this->from . $eol;
$data .= 'Subject: ' . $subject . $eol;
$data .= 'To: "' . $to . '" <' . $to . '>' .
$eol; $data .= 'X-Priority: 1 (High)' . $eol;
// you can comment this one out if you want a normal email.
$data .= 'X-Mailer: ' . $eol;
$data .= 'MIME-Version: 1.0' . $eol;
$data .= 'Content-Type: text/plain; charset="ISO-8859-1"' . $eol;
$data .= 'Content-Transfer-Encoding: 8bit' . $eol . $eol;
$data .= $body . $eol;

$has_error = false;
if ( ( $smtp = fsockopen ( 'ssl://' . $host, $port, $errno, $errstr, 5 ) ) ) {
fputs ( $smtp, 'HELO ' . $host . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) { $
has_error = true;
}
sleep(2);
fputs ( $smtp, 'AUTH LOGIN' . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
sleep(2);
fputs ( $smtp, base64_encode ( $this->from ) . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
sleep(2);
fputs ( $smtp, base64_encode ( $this->password ) . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
sleep(2);
fputs ( $smtp, 'MAIL From: <' . $this->from . '>' . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
} sleep(2);
fputs ( $smtp, 'RCPT To: <' . $to . '>' . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
sleep(2);
fputs ( $smtp, 'DATA' . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
sleep(2);
fputs ( $smtp, $data . $eol . '.' . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
sleep(2);
fputs ( $smtp, 'QUIT' . $eol );
if ( ! $this->test_return ( $smtp, $error ) ) {
$has_error = true;
}
fclose ( $smtp );
}
return !$has_error;
}

function test_return ( $res, &$error ) {
$out = fread ( $res, 1 );
$len = socket_get_status ( $res );
if ( $len > 0 ) {
$out .= fread ( $res, $len['unread_bytes'] );
}

//echo $out;
if ( preg_match ( "/^5/", $out ) ) {
$error = $out;
return false;
}
return true;
}

}


3.) In your controller, you can now use it this way:

$mail = myMailer(“username@gmail.com”, “thisshouldbeaveryhardpassword!@#$!@#$!@#$”, “From Me!”);
if($mail->send(“spam_victim@somewhere.com”, “This Is Spam”, “<div style=’font-size:300px’>GET THIS</div>”)) echo “Mail Sent!”;
else echo “Mail is not sent!”;


And you’re done!

8.3.09

Sending HTTP Posts through JAVA

I found this truly helpful tip :D

http://www.javaworld.com/javatips/jw-javatip34.html?page=1

From the site:

    URL              url;
    URLConnection   urlConn;
    DataOutputStream    printout;
    DataInputStream     input;
    // URL of CGI-Bin script.
    url = new URL (getCodeBase().toString() + "env.tcgi");
    // URL connection channel.
    urlConn = url.openConnection();
    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);
    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);
    // No caching, we want the real thing.
    urlConn.setUseCaches (false);
    // Specify the content type.
    urlConn.setRequestProperty
    ("Content-Type", "application/x-www-form-urlencoded");
    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());
    String content =
    "name=" + URLEncoder.encode ("Buford Early") +
    "&email=" + URLEncoder.encode ("buford@known-space.com");
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();
    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());
    String str;
    while (null != ((str = input.readLine())))
    {
    System.out.println (str);
    textArea.appendText (str + "\n");
    }
    input.close ();



Thanks to the person who posted this :D