I have a website with an SSL.
I would like to encrypt outgoing emails from my server. I've been digging around at this and I really don't know where to begin.
Here is my PHP email script so you have an idea of what I'm using:
public function email($to, $title, $message){
$from = "angela#mysite.com";
$headers = "From: {$from}\r\n";
$headers .= "X-Confirm-Reading-To: {$from}\r\n";
$headers .= "Reply-To: {$from}\r\n";
$headers .= "Organization: InfiniSys, inc.\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$subject = $title;
mail($to, $subject, $message, $headers);
}
Ubuntu 14.04
I'm not sure if this is a server setting or programming config.
Very interesting post: (can't remember where I got it)
<?php
// Setup mail headers.
$headers = array("From" => "someone#example.com",
"To" => "someone-else#example.com",
"Cc" => "spam#somewhere.org",
"Subject" => "Encrypted mail readable with most clients",
"X-Mailer" => "PHP/".phpversion()
);
// Get the public key certificate.
$pubkey = file_get_contents("C:\test.cer");
// Remove some double headers for mail()
$headers_msg = $headers;
unset($headers_msg['To'], $headers_msg['Subject']);
$data = <<
This email is Encrypted!
You must have my certificate to view this email!
Me
EOD;
//write msg to disk
$fp = fopen("C:\msg.txt", "w");
fwrite($fp, $data);
fclose($fp);
// Encrypt message
openssl_pkcs7_encrypt("C:\msg.txt","C:\enc.txt",$pubkey,$headers_msg,PKCS7_TEXT,1);
// Seperate headers and body for mail()
$data = file_get_contents("C:\enc.txt");
$parts = explode("\n\n", $data, 2);
// Send mail
mail($headers['To'], $headers['Subject'], $parts[1], $parts[0]);
// Remove encrypted message (not fot debugging)
//unlink("C:\msg.txt");
//unlink("C:\enc.txt");
?>
Try Using PHPMAILER It's really Easy
You can find all Username and password from your Cpanel Email Account option
$to= "example#gmail.com";
require 'phpmailerlibrary/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#example.com'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('support#twekr.com', 'example Inc.');
$mail->addAddress($to); // Add a recipient
$mail->addReplyTo('support#example.com', 'Support');
$mail->addCC($to);
$mail->addBCC($to);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject of Email';
$mail->Body = 'Content of your html email';
$mail->AltBody = 'Please Upgrade Your Browser to view this email';
if(!$mail->send()) {
echo "Unable to send email"; exit;
}
You should Use TLS as it is also encryption method 90% website uses including google too.
Check out this post where a user suggests to use PHP Mailer.
You can use phpmailer to send outgoing messages through gmail's SMTP server (smtp.gmail.com), and it has options to connect to the SMTP server by SSL. phpmailer is very simple to setup - just a few PHP files to copy to your server.
Here is a great tutorial
If you don't want to use a third library, you'll need to communicate with a SMTP server using socket and send all the commands manually.
Check out this RFC to know how the protocol works
Related
I try to send mail using php mail function. but I getting error like:
PHP Warning: mail(): SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server. in C:\Inetpub\vhosts\qubedns.co.in\httpdocs\Codes\design\rnp\mailsend.php on line 21
Here is my PHP SCRIPT:
<?php
$toEmail = 'bikash336#gmail.com';
$subject = 'hello';
$message = 'Users are able to send emails to local domains and addresses but are unable to send email to any external address.';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <sales#leonwallet.com>' . "\r\n";
$Status = mail($toEmail,$subject,$message,$headers);
if($Status){
echo '1';
}else{
echo '0';
}
?>
Here is my Server configuration: http://qubedns.co.in/codes/php/
What's wrong with me?
I have found the solution with PHPMailer.
Its work for me.
<?php
require('mailserver/PHPMailerAutoload.php');
require('mailserver/class.phpmailer.php');
require('mailserver/class.smtp.php');
require('mailserver/class.pop3.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->Username = 'sales#leonwallet.com';
$mail->Password = 'password';
$mail->From = 'sales#leonwallet.com';
$mail->FromName = 'Leon Sales Team';
$mail->AddAddress($to);
$mail->AddReplyTo('sales#leonwallet.com', 'Leon Sales Team');
$mail->Subject = $sub;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = $message;
if(!$mail->Send()){
echo "1"; // ERROR
}else{
echo "0"; // SUCCESS
}
?>
Here is a solution. For me he solved the problem
When I changed the environment, the TO parameter was configured with the domain of my test / development project.
I changed the TO email to the production email and the error disappeared.
Here is the solution: http://www.marathon-studios.com/blog/php-mail-503-error/
I am sure most of you have already done with kind of thing but i am unable to use correct charset for subject with html format on this code for phpmailer somehow eventhough i've tried hundereds of way. Could you guys catch what could be wrong with the code below?
If i replace "Content-Type: text/html" to "Content-Type: text/plain" then utf8 works fine for only message body but not for subject still gets bad header warning besides mail does not seem as html format either. And what should i write to use an external mail server into this code ?
$message = "Hi $uname,\r\n\r\n\r\n";
$message .= "$passwordLink\r\n\r\n\r\n";
$message .= "you just hit password recovery request\r\n;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Password Service <service#domain.com> \n";
$headers .= "To-Sender: \n";
$headers .= "X-Mailer: PHP\n";
$headers .= "Reply-To: noreply#domain.com\n";
$headers .= "Return-Path: noreply#domain.com\n";
$headers .= "Content-Type: text/html; charset=utf-8";
$host = "mailgw.domain.com";
$subject = "Pass reminder service";
#mail($email,$subject,$message,$headers);
return str_replace("\r\n","<br/>",$message);
Thanks in advance,
Pass your $message and $subject through this function http://php.net/manual/en/function.utf8-encode.php.
To use external server I would recommend to use PHPMailer class https://github.com/PHPMailer/PHPMailer, so you could do something like this:
$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;
...
I know my php form works here on godaddy server:
http://thespanishlanguageacademy.net/los-angeles/learn-spanish-kids-children/kontact.html
Please test it yourself put your email address in and it will send you a copy.
I copy the same code into a different server. This server is not go daddy. I know php works on this server, but for some reason this form is not working:
http://hancockcollege.us/kontact.html
Here is the php code:
// if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){
// put your email address here scott.langley.ngfa#statefarm.com, slangleys#yahoo.com
$youremail = 'bomandty#gmail.com';
// prepare a "pretty" version of the message
$body .= "Thank You for contacting us! We will get back with you soon.";
$body .= "\n";
$body .= "\n";
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
$body .= "\n";
}
$CCUser = $_POST['EmailAddress'];
// Use the submitters email if they supplied one
// (and it isn't trying to hack your form).
// Otherwise send from your email address.
if( $_POST['EmailAddress'] && !preg_match( "/[\r\n]/", $_POST['EmailAddress']) ) {
$headers = "From: $_POST[EmailAddress]";
} else {
$headers = "From: $youremail";
}
// finally, send the message
mail($youremail, 'Form request', $body, $headers, $CCUser );
}
// otherwise, let the spammer think that they got their message through
First,
Use this headers:
<?php
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
//Fom
$headers .= "From: XXX XXXX XXXXX <xxxx#xxxx.xxx>\r\n";
//Reply
$headers .= "Reply-To: example#example.com\r\n";
//Path
$headers .= "Return-path: example#example.com\r\n";
//CC
$headers .= "Cc: example#example.com\r\n";
//BBC
$headers .= "Bcc: example#example.com,example#example.com\r\n";
?>
Two, Read about PHPMailer and see the next code:
And try with this:
<?php
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.gif');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Maybe you have to initialize manually in the other server the SMTP path, i had a similar problem time ago, setting the correct SMTP fixed my problem.
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "yourmeail#yourdomain.com");
Hello I am learning php where i came to know mail() function i have tried this code
function sendMail()
{
$to = 'Sohil Desai<sohildesaixxxx#gmail.com>';
$from = 'Sohil Desai<sohildesaixxxx#hotmail.com>';
$subject = 'Test Mail';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();
$message = 'This mail is sent for testing.';
$mail = mail($to, $subject, $message, $headers);
if (!$mail) {
return 'Error occured sending mail.';
}
return 'Mail successfully sent.';
}
echo sendmail();
I have tested only for gmail, ymail and hotmail.
This function sends mail in a spam for gmail & hotmail and it won't send mail to ymail.
why it happens??
I am using Ubuntu 12.04 & php version 5.3.10.
Can anyone help me?? Thanks to halpers in advance..
Try adding "to" as a header:
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();
More details can be found at http://www.php.net/manual/en/function.mail.php (example #4).
Verry simple
$to = 'someone#example.com';
$subject = 'the subject';
$message = 'the message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Hope helps you!
Source: http://w3webtools.com/send-email-using-php/
Instead of using build in crappy email() function, make a use of PHPMailer.
It is easy to use and does most of the hard work for You.
Features:
Probably the world's most popular code for sending email from PHP!
Used by many open-source projects: WordPress, Drupal, 1CRM, SugarCRM,
Yii, Joomla! and many more Integrated SMTP support - send without a local mail server Send emails with multiple TOs, CCs, BCCs and
REPLY-TOs Multipart/alternative emails for mail clients that do not read HTML email Support for UTF-8 content and 8bit, base64, binary,
and quoted-printable encodings SMTP
authentication with LOGIN, PLAIN, NTLM, CRAM-MD5 and Google's
XOAUTH2 mechanisms over SSL and TLS transports Error messages in
47 languages!
DKIM and S/MIME signing support Compatible with PHP
5.0 and later Much more!
All you have to do is to download and include this class to your project and then use it as in example:
A Simple Example
<?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->setFrom('from#example.com', '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->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';
}
You'll find plenty more to play with in the examples folder at https://github.com/PHPMailer/PHPMailer.
My simple code below:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: info#mail.com \r\n";
$headers .= 'Bcc: test#mail.com' . "\r\n";
$subject = "Information";
if (mail($email, $subject, $message, $headers)) {
$mail_status = "success";
} else {
$mail_status = "fail";
}
if ($mail_status == "success") echo '{"status":"success"}';
How can I support SMTP support?
As You mentioned PHPMailer in tags. You can use PHPMailer Class for this process.
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional,
//gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$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!";
}
Source: http://phpmailer.worxware.com/index.php?pg=examplebsmtp
SMTP server has to be configured on server, but by PHP, which is a programming language. Ask Server admin to provide you SMTP details.
SMTP information can be set in php.ini or via ini_set()
Example:
ini_set("SMTP","smtp.example.com" );
ini_set('sendmail_from', 'user#example.com');
By the way, if you require SMTP Authentication, mail() does not support it. Try to use other PHP mail library such as SwiftMailer or PHPMailer.