here is my php code to send email.
<?php
class mailer {
public function send_request_mail($to, $msg) {
$from="abcd#xyz.com";
$headers = 'MIME-Version: 1.0' . "\r\n".'Content-type: text/html; charset=iso-8859-1' . "\r\n".'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion ();
$message = "ip 192.168.0.9:9035";
$subject = "subject";
mail ( $to, $subject, $message, $headers );
}
}
$mail=new mailer();
$mail->send_request_mail("abcd#xyz.com", "msg");
?>
sometimes its works(for some messages).when i try to send an ip address like above,it fails.help me
Hope you are doing Well.
PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed [mail function].
Windows users should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.
The configuration for Windows should look something like this:
[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net
; For win32 only
sendmail_from = webmaster#tutorialspoint.com
Linux users simply need to let PHP know the location of their sendmail application. The path and any desired switches should be specified to the sendmail_path directive.
The configuration for Linux should look something like this:
[mail function]
; For Win32 only.
SMTP =
; For win32 only
sendmail_from =
; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i
PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.
mail( to, subject, message, headers, parameters );
Example:
Following example will send an HTML email message to xyz#somedomain.com copying it to afgh#somedomain.com. You can code this program in such a way that it should recieve all content from the user and then it should send an email.
<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
$to = "xyz#somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header = "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
Hope this will be usefull to you !!! Cheers !!
Waiting for your positive comments !!!
You may want to use something like PHPMailer because there is a good chance your email will end up in spam or junk box when you use mail() function.
Here is a sample code using PHPMailer,
<?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 = 'abcd#xyz.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->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'ip 192.168.0.9:9035';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Related
$to = 'abc#xyz.com';
$subject = 'Feedback';
$finalmessage = "";
$from = 'def#ghi.com';
$finalmessage = $name . $address . $phnum . $email . $feedback;
$finalmessage = wordwrap($finalmessage, 70);
$mail=mail($to,$subject,$finalmessage,"From: $from\n");
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
Thats the code. At the end it displays "Thank you for using our mail form", but i am not receiving any mail. Any ideas whats going wrong?
if(isset($_POST['Submit'])){
$to="email";
// this is your Email address
$from = $_POST['Email_Address']; // this is the sender's Email address
$first_name = $_POST['Full_Name'];
$tel_num=$_POST['Telephone_Number'];
$msg=$_POST['Your_Message'];
$subject = "Full Name : ".$first_name;
$headers = "From: ".$first_name." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$message="Full Name : ".$first_name."<br><br>Telephone Number : ".$tel_num."<br><br>Message : ".$msg;
$bericht = nl2br($message);
mail($to,$subject,$message,$headers);
}
You need to configure SMTP settings of your local server in php.ini file as follows:
[mail function]
; For Win32 only.
SMTP = localhost // your smtp server
smtp_port = 25
Or you should give try to Swift Mailer or PHP Mailer
Try using fake sendmail to send emails in a windows enviroment.
http://jesin.tk/using-sendmail-on-windows/
Use php mailer() function
You can use PHPmailer :
http://phpmailer.codeworxtech.com/
Now
use following code -
<?php
require("class.phpmailer.php"); // give proper path of folder if needed
$mail = new PHPMailer();
session_start();
ob_start();
php?>
<your mail body goes here>
<?php
$body=ob_get_contents();
ob_end_clean ();
$to = 'abc#xyz.com';
$subject = 'Feedback';
$finalmessage = "";
$from = 'def#ghi.com';
$finalmessage = $name . $address . $phnum . $email . $feedback;
$finalmessage = wordwrap($finalmessage, 70);
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->IsHTML(true);
$mail=mail($to,$subject,$finalmessage,"From: $from\n");
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
You need to configure your mail SMTP in php.ini
It's quite simple, what is your ISP? For example, in comcast, just search in Google "comcast smtp address and port"
Take note of the SMTP address and Port, then go to your php.ini
Search for this configuration:
[mail function]
; For Win32 only.
SMTP = localhost // your smtp server
smtp_port = 25
Change the address with the SMTP and smtp_port. Then you should be able to send emails in your localhost
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.
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);
So I wrote a php script that sends a user a temporary password for when the forget their password so they can login and change it. The script works fine, and the email gets sent with all the correct information. The thing i want to change is who it is getting sent by. I want to use google email app for websites to send those emails, rather the emails are getting sent by my webserver. Here's what the sending part of my script looks like:
$email_to = $_POST["email"];
$email_from = "Admin#domain.com";
$email_subject = "Account Information Recovery";
$email_message = "Here is your temporary password:\n\n";
$email_message .= "Password: ".$password."\n";
$email_message .= "\nPlease log into your account and immediately change your password.";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
However when I receive the email, it comes from Admin#webserver. How do I use google's email app to send these emails?
Probably best to use PHPMailer:
$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; //1 for debugging, spits info out
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; //needed for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'google_username';
$mail->Password = 'google_password';
$mail->SetFrom($email_from, 'Your Website Name');
$mail->Subject = $email_subject;
$mail->Body = $email_message;
$mail->AddAddress($email_to);
$mail->Send();
Note: This example uses SMTP directly to send the email, which will correct the issue, but if the host has fsockopen disabled it will not work.
I would suggest Swiftmailer. It's got a very nice and well-documented API, and supports all different kinds of transports.
From the docs:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);