I'm running fedora core 9 with php 5.2.6. I want to send a simple email from a form using the php mail command.
Do I need to configure my system to be an smtp server? If so do I have to load something like pear?
I've read that pear is installed by default. however when I go to /usr/lib/php/pear , the folder is empty.
When I go to install pear with yum install php-pear none of the mirrors are available.
Does anyone have any suggesions on what mail service to use? If it's installed by default and where I can figure out how to configure.
Thanks!
Joe
This the code I'm using:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = "587"; // set the SMTP port for the GMAIL server
$mail->Username = "joestestemail#gmail.com"; // GMAIL username
$mail->Password = "joeiscool"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('joestestemail#gmail.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('joe12345#mailcatch.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
I would strongly recommend using a library like PHPMailer to send emails.
You can use it with the server's own mail service or with any other server on the internet (your ISPs, Gmail, etc).
For a better idea, check this example from their website:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Edit:
Following up on your comment..
For a simple GMail example, try this:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('name#yourdomain.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Edit2:
The reason why you're getting this error is because you have an extra ' on your string:
Replace
$mail->Host = "'smtp.gmail.com";
with:
$mail->Host = "smtp.gmail.com";
The easiest way to get mail sending up and running is to install and configure postfix. You can use:
yum install postfix
And check out the documentation here:
http://www.postfix.org/docs.html
You can use the LAMPStack from BitNami. It comes with an easy installer that installs everything you need and you'll be running in minutes.
Related
I am using PHPMailer and smtp.gmail.com to send emails to my users.
Emails are sent, no problem with that, but on the client side, in the sender email address, there is showing my servers host email address, not my email address that is set with PHPMailer->SetFrom(). I want to set my email address as sender email.
I'm using php 5.4 & PHPMailer 5.2.4
My code is given below :
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "myemail#gmail.com";
$mail->Password = "PASSWORD";
$mail->AddAddress('receiver#email.com', 'John Doe');
$mail->SetFrom('myemail#email.com', 'My Name');
$mail->addReplyTo('myemail#gmail.com', 'My Name');
$mail->Subject = 'PHPMailer Subject';
$mail->MsgHTML('This is the body');
$mail->Send();
echo "Message Sent";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
?>
This is a gmail limitation - it does not let you set arbitrary from addresses, though you can set fixed aliases in your gmail preferences. This is covered in the PHPMailer documentation. It's also a reasonable restriction - otherwise you're probably forging the from address. You can always set a reply-to address if it's reply routing you're concerned about.
I´m having trouble sending a e-mail with the PHPMailer class, after submit form i have a message mail send ok but i don't recive any mail.
I guess the problem is with the SMTP authentication, but I couldn´t find the problem.the source application are stored in a distant server with ip adress:175.2.3.69 and i use outlook count to send mail
The code with problem is:
require_once('../libs/PHPMailer/class.phpmailer.php');
//Ensuite on débute l'envoi de mail
$mail = new PHPmailer();
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "messagerie.abc.a.fr"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username.name#a-bc.fr"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->AddReplyTo('username.name#a-bc.fr', 'First Last');
$mail->AddAddress('username.name#a-bc.fr', 'John Doe');
$mail->SetFrom('username.name#a-bc.fr', 'First Last');
$mail->AddReplyTo('username.name#a-bc.fr', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
PHPMailer will not throw exceptions unless you pass true to the constructor, like $mail = new PHPmailer(true);, so your code will not cause any exceptions to catch, nor report any errors. I suggest you set $mail->SMTPDebug = 3; to get more feedback on the problem.
I am using php-mailer to send email.
The mail goes succesfully however in each email I get "mailed-by: gamil.com" and signed-by: "gmail.com" in mail header.
How can I remove mailed-by and signed-by tag from email ?
Here is my code
require_once('././php_mailer/class.phpmailer.php');
$body = html($msg);
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = 'smtp.gmail.com'; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com'; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'nabina.smartmobe#gmail.com'; // SMTP account username
$mail->Password = '******'; // SMTP account password
$mail->AddAddress($to, 'Nabina');
$mail->SetFrom('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->AddReplyTo('no-reply#nayacinema.com.np', 'no-reply#nayacinema.com.np');
$mail->IsHTML(true);
$mail->Subject ='NayaCinema: '.$subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
if($attachment){
$mail->AddAttachment($attachment);
}
if($mail->Send()){
//echo 'sent'; die;
return true;
}else{
//echo ' not sent'; die;
return false;
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Thank You
I have been testing the following code for hours. The email will send to the addresses added through $mail->AddAddress() and in the received email it states the cc but the person cced does not receive the email. I have looked everywhere and can not find a solution to why this is happening. I have run tests and all variables are being submitted to this code properly.
My server is running Linux Red Hat
My Code:
require_once('../smtp/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server 465 or 587
$mail->Username = $username; // GMAIL username
$mail->Password = $password; // GMAIL password
// Add each email address
foreach($emailTo as $email){ $mail->AddAddress(trim($email)); }
if($cc!=''){ foreach($cc as $email){ $mail->AddCC(trim($email)); } }
if($bcc!=''){ foreach($bcc as $email){ $mail->AddBCC(trim($email)); } }
$mail->SetFrom($emailFrom, $emailName);
$mail->AddReplyTo($emailFrom, $emailName);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($content);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo'1';exit();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
$address = "xxxxx#gmail.com";
$mail->AddAddress($address, "technical support");
$address = "yyyyyy#gmail.com";
$mail->AddAddress($address, "other");
$addressCC = "zzzzzz#gmail.com";
$mail->AddCC($addressCC, 'cc account');
$addressCC = "bcc#gmail.com";
$mail->AddBCC($addressCC, 'bcc account');
i want to send emails with phpmailer.
for doing that i searched google and i found the blow link :
http://phpmailer.worxware.com/
so i download their libraries from here (by download link in their site , php 4-5):
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/0.90/
after that i refer to this example : (mean advanced using smtp)
http://phpmailer.worxware.com/index.php?pg=exampleasmtp
<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
but i could n't find class.phpmailer.php file for this line -> require_once('../class.phpmailer.php'); from dowloaded file.
did i miss something?
would u plz help me for that?
best regards.
The "../" means "one directory level higher". That means that it should look for the file class.phpmailer.php in the directory one higher than that where this script is executing from. If that's not where you saved class.phpmailer.php relative to your code, then adjust the path.
Try this one.
<?php
include "/home/tnehme/public_html/smtpmail/classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
I'm using PHPMailer with the 5.5.10 and its working really well.
It was very easy to get up and running. Copy files into a directory (alongside your PHP files)
'include' it in your script... and use the example code they give you as a basis for your email.
To get the files visit:
https://github.com/PHPMailer/PHPMailer
good luck.
I also here good things about SwiftMailer.