How to send email from PHP without SMTP server

What is PHP mail And send email from PHP without SMTP ?

PHP mail is the built-in PHP function that is used to send emails from PHP scripts.

The mail function accepts the following parameters:

  • Email address
  • Subject
  • Message
  • CC or BC email addresses
  • It’s a cost-effective way of notifying users of important events.
  • Let users contact you via email by providing a contact us form on the website that emails the provided content.
  • Developers can use it to receive system errors by email
  • You can use it to email your newsletter subscribers.
  • You can use it to send password reset links to users who forget their passwords
  • You can use it to email activation/confirmation links. This is useful when registering users and verifying their email addresses

 

In this tutorial, you will learn-

  • Why/When to use the PHP mail
  • Simple Mail Transmission Protocol
  • Sanitizing email user inputs
  • Secure Mail

 

Why/When to use the mail PHP

Sending mail using PHP

The PHP mail function has the following basic syntax

mail($to_email_address,$subject,$message,[$headers],[$parameters]);

 

Yes, PHPMailer is a very good choice.

For example, if you want, you can use Google free SMTP server (it’s like sending from your Gmail account.), or you can just skip the SMTP part and send it as a typical mail() call, but with all the correct headers etc. It offers multipart e-mails, attachments.

 

easy to set up too.

 

$mail = new PHPMailer(true);

//Send mail using gmail

if($send_using_gmail){

$mail->IsSMTP(); // telling the class to use SMTP

$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 for the GMAIL server

$mail->Username = "your-gmail-account@gmail.com"; // GMAIL username

$mail->Password = "your-gmail-password"; // GMAIL password

}

//Typical mail data

$mail->AddAddress($email, $name);

$mail->SetFrom($email_from, $name_from);

$mail->Subject = "My Subject";

$mail->Body = "Mail contents";

try{

$mail->Send();

echo "Success!";

} catch(Exception $e){

//Something went bad

echo "Fail - " . $mail->ErrorInfo;

}

“send email through php mail without smtp” Code Answer

ini_set('display_errors',1);

ini_set('display_startup_errors',1);

error_reporting(-1);

session_start();

$to = 'toSenderEmail@xxxxx.xxxxx';

$subject = 'Subject xxxxx xxxxxx';

$headers = "MIME-Version: 1.0rn";

$headers .= "Content-Type: text/html; charset=charset=utf-8rn";

$headers .= "From: " . $_POST['email'] . "rn";

//$headers .= "Reply-To: ". $_POST['email'] . "rn";

$headers .= "CC: xxx@xxxxx.xxxrn";

$message = "<html><body>";

$message .= '<table style="border-color: #666; background: #eee; cellpadding="10">';

$message .= "<tr><td><strong>Name:</strong> </td><td>" . $_POST['username'] . "</td></tr>";

$message .= "<tr><td><strong>Email:</strong> </td><td>" . $_POST['email'] . "</td></tr>";

$message .= "</table>";

$message .= "</body></html>";

echo $message;

//echo $headers;

$response=mail($to, $subject, $message, $headers);

if($response==1){

echo "<script language='javascript' type='text/javascript'>

window.location = 'index.html';

</script>";

}else{

echo

"<script language='javascript' type='text/javascript'>

alert('mail send failed');

</script>";

}

 

Any ideas?

I found the solution:

 

1 – In the PHP Settings -> PHP Extensions you must compile into the EXE the php_curl.dll (agree to include the ssleay32.dll and the libeay32.dll), and the php_openssl.dll

2 – Download the PhpMailler (I’m using the 5.1)

3 – Create or use a valid gmail account

4 – Change the settings on the next example :

 

require("PHPMailer_v5.1/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();  // Telling the class to use SMTP

$mail->SMTPAuth = true;

$mail->SMTPSecure = "ssl";

$mail->Host     = "smtp.gmail.com"; // SMTP server

$mail->Username = "yourmail@gmail.com"; // "The account"

$mail->Password = "yourpasswordhare"; // "The password"

$mail->Port = 465; // "The port"

$mail->Subject  = "My first email "; // "The subject"

$mail->Body     = "This is a test"; // "The message."

$mail->WordWrap = 100; // "The lenght of the text."

if(!$mail->Send()) {

echo "Message was not sent.";

echo "Mailer error: ". $mail->ErrorInfo;

} else {

echo "Message has been sent.";

}

 

Secure Mail

Emails can be intercepted during transmission by unintended recipients.

This can exposure the contents of the email to unintended recipients.

Secure mail solves this problem by transmitting emails via Hypertext Transfer Protocol Secure (HTTPS).

HTTPS encrypts messages before sending them.

 

Summary

The PHP built in function mail() is used to send mail from PHP scripts

Validation and sanitization checks on the data are essential to sending secure mail

The PHP built in function filter_var() provides an easy to use and efficient way of performing data sanitization and validation

 

You may like

Scroll to Top