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:

Basic example use:


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

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

$mail->AddReplyTo("","First Last");

$mail->SetFrom('', 'First Last');

$mail->AddReplyTo("","First Last");

$address = "";
$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->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

PHP Parse error: syntax error, unexpected $end

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

How to set php.ini values using .htaccess

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

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...