Email mail() Alternative

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
Using the built in PHP mail() function here at X10 is all very well for most people but sometimes users run into the problem of mail 'bouncing' from the likes of Hotmail and Yahoo. This is because of the shared servers previous misuse at the hands of spammers has resulted in them being blacklisted and there is little that can done about it.

Thankfully there is way to send email from a website without using the mail() function with the aid of PHP sending the email via Google Gmail and it is remarkably simple to do.

Gmail of course does have limits for how many messages can be sent each day (think it's around 500) to stop spamming. For a website contact form or small newsletter it should be more than enough.

For more information Google "Php Mailer Class"
the download link for phpmailer is http://sourceforge.net/projects/phpmailer/

PHP:
<?php

// example on using PHPMailer with GMAIL

include("class.phpmailer.php");
include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded

$mail             = new PHPMailer();
$YourName            = "youremail@gmail.com";     
$YourPass            =  "Your Gmail Password"; 

$SendMailTo          ="somebody@wherever.com";    // email address to send message to
$body             = file_get_contents('contents.html'); // message contents using remote HTML file
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;                      // enable SMTP authentication
$mail->SMTPSecure = "ssl";                     // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";          // sets GMAIL as the SMTP server
$mail->Port       = 465;                        // set the SMTP port

$mail->Username   = $YourName;                    
$mail->Password   = $YourPass;                          

$mail->From       =  $YourName;    
$mail->FromName   = "Webmaster";                
$mail->Subject    = "This is the subject";        // put whatever you like
$mail->AltBody    = "This is the body when user views in plain text format"; //Text Body // put whatever you like
$mail->WordWrap   = 50;                         // set word wrap

$mail->MsgHTML($body);

$mail->AddReplyTo($YourName,"Webmaster");// put whatever you like

$mail->AddAttachment("/path/to/file.zip");             // optional attachments
$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // optional attachments

$mail->AddAddress( $SendMailTo,"First Last");

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message has been sent";
}

?>
Simply fill in your own Gmail account details, the recipients email address and add the file message.html then away you go it really is that easy.

Attachments for zip folders or images are of course optional just comment out the lines if you do not want to use them.

Have fun and please use responsibly.
Google is like a sleeping bear, if you are quiet and do not annoy it then it will leave you alone. Wake it and it will eat you alive.
 

techairlines

x10 Flyer
Community Support
Messages
2,867
Reaction score
165
Points
63
Thanks for this tutorial. I'm sure many will find it useful.

I would also like to add that this works with Google Apps accounts so you can send mail using @yourowndomain.com. Simply enter your Google Apps email address (such as admin@x10email.com) and password and it will work just like a normal Gmail account.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Thanks. Have been using PEAR::Mail but that involves downloading PEAR and other requirements.
 
Top