Send email from localhost to smtp - php

i am trying to send the contact form data to email using smtp server . but its not working and i dont know how to configure smtp, i googled it but didn't found any good solution. my php code for email sending is :
// Please specify your Mail Server - Example: mail.example.com.
ini_set("SMTP","ssl://smtp.gmail.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","465");
// Please specify the return address to use
ini_set("sendmail_from","chadhar313#gmail.com>");
$to ="chadhar313#yahoo.com";
$yourname = trim($_POST['yourname']);
$email = trim($_POST['email']);
$subject = trim($_POST['subject']);
$message = trim($_POST['message']);
$header = "Contact Form";
$message = "Name: $yourname \r\n Email: $email \r\n Subject:
$subject \r\n Message: $message";
$headers = "From:" . $yourname;
$mailsent = mail($to, $header, $message, $headers);
if($mailsent) {
$sent = true;

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 the /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

Related

how to send smtp mail from localhost

I always get this message when trying to send email from my local host.
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
below is my code: please help
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("class.phpmailer.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer#bradm.inmotiontesting.com
// pass: password
$mail->Username = "project#reliable.com.pk"; // SMTP username
$mail->Password = "Nov112014"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("mani9418#gmail.com", "Usman Ali Siddiqui");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website Etutionhub!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Open the php.ini. For XAMPP, it is located in C:\XAMPP\php\php.ini. Find out if you are using WAMP or LAMP server.
Note: Make a backup of php.ini file.
Search [mail function] in the php.ini file.
You can find like below.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster#localhost
Change the localhost to the smtp server name of your ISP. No need to change the smtp_port. Leave it as 25. Change sendmail_from from postmaster#localhost to your domain email address which will be used as from address.
So for me, it will become like this.
[mail function]
; For Win32 only.
SMTP = smtp.example.com
smtp_port = 25
; For Win32 only.
sendmail_from = info#example.com
Restart the XAMPP or WAMP(apache server) so that changes will start working.
Now try to send the mail using the mail() function.
mail("example#example.com","Success","Great, Localhost Mail works");
Mail will be sent to example#example.com from the localhost with Subject line "Success" and body "Great, Localhost Mail works".
You can add this line
$mail->SMTPSecure = "ssl";
after
$mail->SMTPAuth = true;

Php mail not Send sometimes

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';
}

Not able to send mail using mail function in php

would anyone be able to help me with my question..I am not able to send mail using mail() in php..Here is my code:
if (isset($_POST["from"])) {
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
mail("test#example.com",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
while i am running this program in localhost,output showing as "Thank you for sending us feedback" , but not getting any mail in test#example.com.
Check your php.ini cofiguration file and add the mail server config:
SMTP = server ; mail server
smtp_port = 25 ; port
sendmail_from = your#email.com ;
Or
Use PHPMailer https://github.com/PHPMailer/PHPMailer to send via gmail, yahoo or any external mail server.

PHP mail function is not working?

$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

Using Return-path when sending mail through SMTP using PHP

Is there a way to set the Return-path when sending mail through authenticated SMTP using PHP?
I want bounce mails to be caught by another e-mail address than the "from" address.
I know that there is a way to do this with the "normal" PHP mail() function (by setting the "-f" flag in the 5th parameter), but I have no clue how to manage this with SMTP.
Also tried PEAR's Mail-package, but setting Return-path in the headers didn't do the job.
Set the fourth mail()-parameter (additional_headers) to "Return-path:mybouncereceiver#example.com".
Example:
$to = "to#example.com";
$from = "from#example.com";
$bounce = "mybouncereceiver#example.com";
$subj = "mysubject";
$message = "blah";
$headers = "From:$from\r\nReturn-path:$bounce"
mail($to, $subj, $message, $headers);
You can see that you separate multiple additional_headers with \r\n (newlines).
See also: http://php.net/manual/en/function.mail.php
Here's what you need to do.
You need to set 'Return-Path' in the Headers to the email you want to use as your bounce email. This worked for me.
For example :
$headers['From'] = 'richard#example.com';
$headers['To'] = 'joe#example.com';
$headers['Subject'] = 'Test message';
$headers['Return-Path'] = 'bounce#example.com';

Categories