how to send mail in php? - php

can anyone provide me the way of sending mail through php with attached object as well .plz i am new in this kindly help me in this. is there any server to be installed for this? the mail should this on any email account aswell plz help me in this.can any one provide me the link of tutorial i used the tutorial HERE it display me the error Fatal error: Call to undefined function IsSMTP() in C:\wamp\www\EMS3\mail.php on line 13 plz help me in this

There is an error in the example provided there. Use the following code.
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
update you also need to set the external SMTP server your using. if your using google. i believe its smtp.gmail.com
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Secure = "ssl";

You can use an external PHP library for sending mail, which I have used internally on a localhost, but still configured the parameters to send to external sources. The package is Swift Mailer.

In localhost you can't send any mail. After hosting only this is possible.

Related

How to send mail using php to gmail account using SMTP

I am trying code in php to send mail using SMTP.I am using xampp server to run php code. I am sending mail from neelabhsingh1986#gmail.com to neelabhsingh1000#gmail.com. I got the php code from this site and github. But I am getting message like
SMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
Php code to send mail
<?php
require("D:xampp/htdocs/PHPMailer_5.2.0/class.PHPMailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "neelabhsingh1986#gmail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "neelabhsingh1986#gmail.com";
$mail->FromName = "Neelabh Singh";
$mail->AddAddress("neelabhsingh1000#gmail.com"); // name is optional
$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 = "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. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
In my setup I also have this:
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
Note that you will probably need to authorise this action on your account. You'll get an email from gmail with a link.
This the answer for my post. Please see the link. Download PHPMailer_5.2.0.zip and unzip. I am using xampp server and I created folder in D:\xampp\htdocs\ with name phpMail. I copied these two files(class.phpmailer.phpclass.smtp.php ) from PHPMailer_5.2.0 to phpMail. Now make file sendMail.php and paste following code.
<?php
require("class.PHPMailer.php");
$mail = new PHPMailer();
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourEmailAddress#gmail.com"; // SMTP username, sender email address
$mail->Password = "yourGmailPassword"; // SMTP password
$mail->From = "yourEmailAddress#gmail.com";
$mail->FromName = "YourName";
$mail->AddAddress("Receiver#gmail.com"); // Write the email of receiver. Who will get the mail.
$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 = "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. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
After writing the code your have to run server. In My case It was http://localhost/phpMail/sendMail.php. When You run this code from browser the you will get one mail regarding this app like Google Account: access for less secure apps has been enabled. When you click this link they asked for permission, Access for less secure apps click yes if you want. Thanks to #rjdown for help.

PHP mailer change sender address using yahoo.com

I am trying to change the sender address of the mail. Firstly I tried by gmail, then I let to know that by gmail its not possible now I am trying using Yahoo, and I got following error:-
SMTP ERROR: MAIL FROM command failed: 553 From address not verified.
Getting blank here I dont understand what do I do? Any help will be appreciated. following is my code:-
<?php
//date_default_timezone_set('Etc/UTC');
require('PHPMailerAutoload.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = "plus.smtp.mail.yahoo.com";
//$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'myyahooid#yahoo.com';
$mail->Password = 'xyz';
$name='Test Name';
$mail->Sender='thishavetoset#domain.com';
//$mail->From = 'sender#senderdomain.com';
//$mail->FromName = 'Sender Name';
$mail->SetFrom('thishavetoset#domain.com', $name, TRUE);
$mail->AddReplyTo('no-reply#mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('myyahooid#yahoo.com', 'title1');
//$mail->AddAddress('abc2#yahoo.com', 'title2'); /* ... */
$fileName='../rough/test.pdf';
$mail->AddAttachment($fileName);
//$mail->send();
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Let me know another solution to set the sender name also if you think, it can't be done this way. Thanks a ton in advance.
The sender email you using should be real email id and as your using Yahoo mail i think then it must registered in yahoo
There are two reasons smtp trows such error are.
first is You are attempting to send email to a domain that is not recognized by this server
second is You are attempting to relay email through this server and have not authenticated
Check if $mail->Sender='thishavetoset#domain.com'; email is set for your domain name your using.
So try to use email which user email password your using.
$mail->Username = 'myyahooid#yahoo.com';
In Gmail you can add that email id as external address from accounts and import tab >> add another email address, You will need details of smtp from the Admin of hosting/domain and add the email id.
once you add the external email address you can use that email id as sender from gmail smtp.

Could not send mail to other accounts apart from my own?

I've downloaded PHPMailer library from github and used the following script to send the mail. I've tried to send email to my own account and it worked. It printed "Message has been sent" and received the email in gmail. But when I tried sending mail to my friend's account, he didn't receive the email. But the script says message has been sent.
<?php
include('PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "example#gmail.com";
$mail->Password = "examplepassword";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("example2#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
I'm using xampp. What's the reason behind the problem, do I need to change any settings in my gmail settings?
EDIT: Now I got mail to second address but after 40 mins. But when I send it first address, it is received immediately. Don't know why? I want to use it for email address verification, 40 mins is very long.
below link will help you:
Sending emails through PHP mail is slow
http://stackoverflow.com/questions/6380477/sending-emails-through-php-mail-is-slow

Sending an email using PHPMailer and GMAIL SMTP

I have read every example out in the web and I still cant seem to connect to the GMAIL SMTP. Here is the code that I am running:
include("phpMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myUsername"; // SMTP username
$mail->Password = "myPassword"; // SMTP password
$mail->SMTPDebug = 1;
$webmaster_email = "webMasterEmail#gmail.com"; //Reply to this email ID
$email="someone#gmail.com"; // Recipients email ID
$name="SomeonesName"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Me";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
I tried setting the port in here and I also have the current set up with the following in the class.smtp.php file:
$host = "ssl://smtp.gmail.com";
$port = 465;
I keep getting the same error and I have made sure that ssl is enabled. The error I get is:
SMTP -> ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it.(10061)
As I mentioned above I had already enabled ssl by getting rid of the ; on the line with php_openssl.dll and restarting Apache. I did some more reading and I found out that some people also had "[PHP_OPENSSL]" before the enabling command. I added it and restarted Apache and everything is working! Thanks for all the comments
In php.ini:
[PHP_OPENSSL]
extension=php_openssl.dll

Php mailer error: SMTP error

I am using php mailer class. I am getting :
SMTP Error: The following recipients failed: s_deshmukh88#hotmail.com
$mail = new phpMailer();
$body = "Hello, this is a test mail.";
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
//$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->Port = 25; // set the SMTP server port
$mail->Host = "localhost"; // SMTP server
$mail->Username = "localhost"; // SMTP server username
$mail->Password = "password"; // SMTP server password
//$mail->SMTPSecure = "tls";
//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("name#domain.com","First Last");
$mail->From = "name#domain.com";
$mail->FromName = "First Last";
$to = "s_deshmukh88#hotmail.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
if($mail->Send()){
echo 'Message has been sent.';
}
What can be the reason ?
Just try to put SMTPAuth to false and Host to'localhost'.
Thanks !! :)
I had this problem using authenticated SMTP. When I tested my mailing from home using xampp but connected to a (shared) server, emails went to users without problems. When I tried the same thing with the PHP on the host server, emails went through to addresses on my account but NOT to external addresses. (Ie. if my address is fred#bloggs.com and I send an email to fred#bloggs.com, it goes through but if I send it to jim#smith.com, it fails) The solution turned out to be to use the full host address when working on my local pc, ie $mail->Host = "mail.fred.bloggs.com", but $mail->Host = "localhost" on the server. May seem perverse but it works.
I just ran into this issue, and in my case, i believe it was the webhost's mail server that was not allowing a non-matching domain name for the From address. So for example, i was using mail.mydomain.com to send mail, but i wanted the from address to be test#test.com
When i switched the from address to test#mydomain.com, then the email was getting sent.

Categories