1// use this library -> https://github.com/PHPMailer/PHPMailer
2
3<?php
4use PHPMailer\PHPMailer\PHPMailer;
5use PHPMailer\PHPMailer\SMTP;
6use PHPMailer\PHPMailer\Exception;
7function sendEmail(){
8 require 'phpmailer/vendor/autoload.php';
9
10 //Create an instance; passing `true` enables exceptions
11 $mail = new PHPMailer(true);
12 $mail->CharSet = 'UTF-8';
13 try {
14 $receiver = 'test@gmail.com';
15 $name = 'Name';
16
17 //Server settings
18 $mail->SMTPDebug = 1; //Enable verbose debug output
19 $mail->isSMTP(); //Send using SMTP
20 $mail->Host = 'tls://smtp.gmail.com'; //Set the SMTP server to send through
21 $mail->SMTPAuth = true; //Enable SMTP authentication
22 $mail->Username = 'username@gmail.com'; //SMTP username
23 $mail->Password = 'PASSWORD'; //SMTP password
24 $mail->SMTPSecure = tls; //Enable implicit TLS encryption
25 $mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
26
27 //Recipients
28 $mail->From = 'test@gmail.com';
29 $mail->setFrom('test@gmail.com', 'Name');
30 $mail->addAddress($receiver, $name); //Add a recipient
31
32 //Content
33 $mail->isHTML(true); //Set email format to HTML
34 $mail->Subject = 'Subject';
35 $mail->Body = 'Body';
36
37 $mail->send();
38 echo 'Message has been sent';
39 } catch (Exception $e) {
40 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
41 }
42}
43?>
1<?php
2
3error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
4
5require_once "Mail.php";
6
7$host = "ssl://smtp.dreamhost.com";
8$username = "youremail@example.com";
9$password = "your email password";
10$port = "465";
11$to = "address_form_will_send_TO@example.com";
12$email_from = "youremail@example.com";
13$email_subject = "Subject Line Here:" ;
14$email_body = "whatever you like" ;
15$email_address = "reply-to@example.com";
16
17$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
18$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
19$mail = $smtp->send($to, $headers, $email_body);
20
21if (PEAR::isError($mail)) {
22echo("<p>" . $mail->getMessage() . "</p>");
23} else {
24echo("<p>Message successfully sent!</p>");
25}
26?>
27
1<?php
2 ini_set( 'display_errors', 1 );
3 error_reporting( E_ALL );
4 $from = "test@hostinger-tutorials.com";
5 $to = "test@hostinger.com";
6 $subject = "Checking PHP mail";
7 $message = "PHP mail works just fine";
8 $headers = "From:" . $from;
9 if(mail($to,$subject,$message, $headers)) {
10 echo "The email message was sent.";
11 } else {
12 echo "The email message was not sent.";
13 }
1
2I migrated an application to a platform without a local transport agent (MTA). I did not want to configure an MTA, so I wrote this xxmail function to replace mail() with calls to a remote SMTP server. Hopefully it is of some use.
3
4function xxmail($to, $subject, $body, $headers)
5{
6 $smtp = stream_socket_client('tcp://smtp.yourmail.com:25', $eno, $estr, 30);
7
8 $B = 8192;
9 $c = "\r\n";
10 $s = 'myapp@someserver.com';
11
12 fwrite($smtp, 'helo ' . $_ENV['HOSTNAME'] . $c);
13 $junk = fgets($smtp, $B);
14
15 // Envelope
16 fwrite($smtp, 'mail from: ' . $s . $c);
17 $junk = fgets($smtp, $B);
18 fwrite($smtp, 'rcpt to: ' . $to . $c);
19 $junk = fgets($smtp, $B);
20 fwrite($smtp, 'data' . $c);
21 $junk = fgets($smtp, $B);
22
23 // Header
24 fwrite($smtp, 'To: ' . $to . $c);
25 if(strlen($subject)) fwrite($smtp, 'Subject: ' . $subject . $c);
26 if(strlen($headers)) fwrite($smtp, $headers); // Must be \r\n (delimited)
27 fwrite($smtp, $headers . $c);
28
29 // Body
30 if(strlen($body)) fwrite($smtp, $body . $c);
31 fwrite($smtp, $c . '.' . $c);
32 $junk = fgets($smtp, $B);
33
34 // Close
35 fwrite($smtp, 'quit' . $c);
36 $junk = fgets($smtp, $B);
37 fclose($smtp);
38}
39