How to send email with SMTP in php
Take a look at PHP Mailer:
https://github.com/PHPMailer/PHPMailer
Example from that page:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Send mail reliably with PHP?
From my experience, PHPMailer is the way to go - https://github.com/PHPMailer/PHPMailer
You need to get access to a SMTP server you will use to send your mail. This could either be the Google SMTP server (a @gmail.com email address) or your company's email server.
A quick example to do this using Google SMTP server is:
1) Install PHPMailer using composer from a terminal / shell:
composer require phpmailer/phpmailer
2) Include the composer generated autoload file and PHPMailer in your php script:
// Load Composer's autoloader
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
3) Send email using:
$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'YOUREMAIL@gmail.com'; // SMTP username
$mail->Password = 'YOUREMAILPASSWORD'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('YOUREMAIL@gmail.com', 'Mailer');
$mail->addAddress('TOADDRESS'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
Read the PHPMailer documentation at: https://github.com/PHPMailer/PHPMailer
How can I send an email using PHP?
It's possible using PHP's mail()
function. Remember the mail function will not work on a local server.
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Reference:
how to send email via SMTP using PHP
Thank You for helping me this is my sample code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test 3 mail</title>
</head>
<body>
<?php
if(isset($_POST['send']))
{
require_once'Swift-5.1.0\lib\swift_required.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
$body = "Name:".$name."<br>Email:".$email."<br>Message:".$message."";
//tarnport
$transport = Swift_SmtpTransport::newInstance('d9.privatewebsolution.com', 465, "ssl")
->setUsername('test@dimain.com')
->setPassword('XXXXX');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Uni-Tech')
->setFrom(array('test@dimain.com' => "Name"))
->setTo($email)
->setBody($body, 'text/html');
$result = $mailer->send($message);
}
?>
<form action="test3.php" method="post">
<label>Name</label>
<input type="text" name="name" /><br />
<label>Email</label>
<input type="email" name="email" /><br />
<label>Message</label>
<textarea name="message"></textarea><br />
<input type="submit" name="send" value="Send" />
</form>
</body>
</html>
PHP send email BCC & CC using SMTP
The carbon copy is an header.
All recipients are recipients in the same way for the mail server, the difference between carbon copy and blind carbon copy is just a matter of declaring it in the headers.
$headers = array(
'Port' => $port,
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'Cc' => $cc
);
How to send an HTML email using SMTP in PHP
The problem most likely lies in the Mail class, but since we don't know what Mail class you're using, it's difficult to answer. If you're not already doing so, I'd really think about using PHPMailer: https://github.com/PHPMailer/PHPMailer
Send HTML Form using SMTP
We only managed to fix our issue by sending it via SMTP but we have to install PHPmailer first on cPanel using the following command
composer require phpmailer/phpmailer
Instructions: https://muftsabazaar.com/blog/post/how-to-install-the-phpmailer-in-cpanel
Then we updated our PHP code as below
<?php
use phpmailer\phpmailer\PHPMailer;
use phpmailer\phpmailer\Exception;
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/SMTP.php';
// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example@domain.com'; // SMTP username
$mail->Password = 'emailpass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('example@domain.com', 'New Form submission on Rentersshield Website');
$mail->addAddress('example@domain.com', 'JohnUser'); // Add a recipient
$mail->addAddress('example@domain.com'); // Name is optional
$mail->addReplyTo('example@domain.com', 'Information');
$mail->addCC('example@domain.com');
$mail->addBCC('example@domain.com');
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "https://rentersshield.org/success/";
// Content
$first_name = $_REQUEST['first_name'] ;
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body =
"First Name: " . $first_name . "\r\n" ;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header( "Location: $thankyou_page" );
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Related Topics
Making a Http Get Request with Http-Basic Authentication
Best Way to Autoload Classes in PHP
Group Array Values Based on Key in PHP
How to Set Current Page "Active" in PHP
How to Store JavaScript Variable Output into a PHP Variable
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute
Error_Reporting(E_All) Does Not Produce an Error
Differencebetween ' and " in PHP
How Would I Stop This Foreach Loop After 3 Iterations
How to Stop PHP Imagick Auto-Rotating Images Based on Exif 'Orientation' Data
How to Maintain Session in Curl in PHP