How to send mails through your website

On shared web hosting server for security reasons it's not possible unauthenticated email sending through - i.e. - the simple mail() function.

To do it, you need to use through an authenticated external SMTP service, and so you need to use a specific library as, i.e., PHPMailer.

Various code examples to use PHPMailer: http://phpmailer.worxware.com/index.php?pg=examples

Basic example use:

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@mydomain.com","First Last");

$mail->SetFrom('name@mydomain.com', 'First Last');

$mail->AddReplyTo("name@mydomain.com","First Last");

$address = "someone@yourdomain.com";
$mail->AddAddress($address, "John Smith");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

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

Was this answer helpful?

Also Read

allow_url_fopen

If enabled, allow_url_fopen allows PHP's file functions (such as file_get_contents() and the...

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings.

If you get following error message:Fatal error: Uncaught exception 'Exception' with message...

PHP Parse error: syntax error, unexpected $end

This is a very common error, typically caused by a missing } used in PHP to denote content...

Limiti funzione php mail()

per motivi di sicurezza sui nostri server di hosting shared Linux la funzione php mail() è...

How to set php.ini values using .htaccess

You can override global values of system variables re-setting them in .htaccessFor each system...