For the past 2 days I\'ve been trying to get a PHP script to send an e-mail and it doesn\'t seem to work. First I\'ve tried the normal php mail function, then PHPMailer and then
Here is the PHP Mailer Example code. Ensure to download the PHPMailer Classes from https://github.com/PHPMailer/PHPMailer
To find your mail server,
If you are using windows operating system:
If you are using Linux
Mostly, smtp servers name are something like smtp.yourdomain.com or mail.yourdomain.com.
Open command prompt try to run following two commands:
You will most probably get response from any one from the above two commands and that might be your smtp server.
If this doesn't work open your cpanel --> go to your mailing accounts -- > click on configure mail account settings-- > there somewhere in the page you will get the information about your smtp server
It will be written like this way may be :
Here's the code.
SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Server settings
// Enable verbose debug output
$mail->SMTPDebug = 2;
// Set mailer to use SMTP
$mail->isSMTP();
// Specify main and backup SMTP servers
$mail->Host = 'mail.your_domain.com;your_backup_smtp.your_domain.com.co.ke';
// Enable SMTP authentication
$mail->SMTPAuth = true;
// SMTP username (This is smtp sender email. Create one on cpanel e.g noreply@your_domain.com)
$mail->Username = 'sender_email@your_domain.com';
// SMTP password (This is that emails' password (The email you created earlier) )
$mail->Password = 'your_password';
// Enable TLS encryption, `ssl` also accepted
$mail->SMTPSecure = 'tls';
// TCP port to connect to. the port for TLS is 587, for SSL is 465 and non-secure is 25
$mail->Port = 25;
//Recipients
$mail->setFrom('sender_email@your_domain.com', 'Company Name');
// Add a recipient
$mail->addAddress('recipient_address@gmail.com', 'Recipient Name');
// Name is optional
//$mail->addAddress('another_email@example.com');
$mail->addReplyTo('info@your_domain.com', 'Information Team');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments (Ensure you link to available attachments on your server to avoid errors)
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'some_imaje.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body in bold and italicized!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>