send mail using mail() in php - php

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.

Related

PHP email encryption

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

php form send email

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");

how to Mail a filled up form to an email address using PHP?

I am trying to send a filled up form to a particular email address. But I have no Idea about it. I know a little PHP coding and as far I have studied online I found that PHP already have a mail() function. but that also have some problems. although most of that I did not understood properly.
here is the things I want to do:
I want to send the filled up form to the particular email address.
I want to send that filled up form to my mobile phone so that i can reply immediately.
I want to send that mail to my inbox only (not spam pr junk).
I am requesting everyone to give me a details information about how I can do so.
thanks in advance..
You can simply use php mail function with all the parameters
for example
mail ( $to ,$subject , $message, $headers );
Where $to, $subject, $message are php variables to contains their respective values.
as you are posting data from the form so you can make $message from form.
like
$message = $_POST['FORM_FIELD_NAME'];
and $headers you set according to your requirement. For example
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Name <myemail#address.com>' . "\r\n";
it can help you..
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "from#example.com";
$mail->AddAddress("id#example.net");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
Use PHPMailer to send mails in PHP
http://phpmailer.worxware.com/‎
PHPMailer wiki page:
https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/UsefulTutorial
Use can use Gmail, Live or any other SMTP server to send mails
Code:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.example.com"; // SMTP server
$mail->From = "from#example.com";
$mail->AddAddress("myfriend#example.net");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

PHP and SMTP server

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.

mail() function not working - How to resolve it?

mail() function is not working on my server. I have used basic mail() code to know whether its the problem of script. Still it didn't send email. Somebody advised me to change the setting on my server to enable mail() function or something like that.
How can I do that? How can I know that my server allows mail() or it runs mail() properly?
Any advice?
If you using phpmailer Library, you cant use mail(). Because it has predefined function. You can check that by visiting PhpMailer Example Page.
PhpMailer use $mail->Send() instead of mail()
Sample code of PhpMailer
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!";
}
If you are on a shared server fisrt i advise you to contact with them if they are responsible for it. if it was sending emails before it could be something you could cause and you may need to change your code.
you did not provide any sample code but here is mine, try it:
$to= "$confoemail";
$subject="Your Contact Request at somewebsite.Com";
$message= "the message to send";
$headers = 'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n".
'From: justin#webmasteroutlet.com' . "\r\n" . //that code here //perfectly works, search if the code is built -in of php.
'Reply-To: justin#webmasteroutlet.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to,$subject, $message, $headers);
What operating system are you running?
On ubuntu, you might try to install a mail server (postfix or sendmail).
apt-get install postfix
Test this on your hosting, if it doesn't work your hosting has mail disabled. which most free services do block. message me for a free small hosting for ur tests.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$name = "somename"; $email="test#test.com"; $phone="1111111111" $subject="test"; $message="the message";
$to = 'info#fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
$phone."</br>Subject: ".$subject.
"</br>Message: ".$message;
//echo $themessage;
mail($to, $subject, $themessage, $headers);

Categories