This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 6 years ago.
Is there a way I can set the email address used by php's mail() function without getting caught by spam filters?
If I don't add a from header the email is sent from apache#ip-17....3.us-west-2.co....rnal which isn't the worst name because most mail services just shorten to Apache. But I would like to have that be noreply.mydomain.com If I add a from header and put anything else it seems to always get caught by spam but not if just left to default.
My setup is PHP 7 on linux Amazon EC2 Micro
You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer .
It allows you to use the mail function or use an smtp server transparently . It also handles HTML based emails and attachments so you don't have to write your own implementation.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.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->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';
}
Related
I am using corephp and i have used hostgator webmail to send email. Email is successfully going to some testing email provider like mailinator but it is not going to gmail,outlook and company email id.It is not in spam/junk as well. There is no error message recorded and it does not create any exception while sending email.
I am not getting what could be the reason?
This is my actual code.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isMail(); // Set mailer to use SMTP
$mail->Host = 'http://********/webmail'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.co';
$mail->FromName = 'Mailer';
$mail->addAddress('To#mailinator.com', 'ToUser'); // Add a recipient
$mail->addAddress('To#mailinator.com'); // Name is optional
$mail->addReplyTo('To#mailinator.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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';
}
Remove This Line from your coding
$mail->addAddress('To#mailinator.com');
You Can Test With Gmail Account you will get mail on your junk mail box. you can report that mail as not a junk mail then you will get mails on outlook.
E.g:
$mail->addAddress('Toemailname#gmail.com', 'ToUser');
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I am trying to send an email with my server. I am using php's mail function. The function is returning true. But I am not receiving any email. I have checked the logs. And even the logs are not showing any error. My domain is "islamerkotha.com". My code is given below -
<?php
$msg = "First line of text\nSecond line of text";
$msg = wordwrap($msg,70);
$headers = "From: test1#islamerkotha.com";
mail("erfan.bashar.13#gmail.com", "My subject", $msg, $headers);
Thank you.
There are many points along the path an email message takes where yours could be failing, but take a look at the PHP mail() function page; it specifically says that the function returns true if the message was accepted for delivery and "it is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination."
EDIT: Look here for more information on PHP error reporting. If you don't see any errors then, look at phpinfo() to see if the mail() function is even enabled. If it is, then it's time to start looking further downstream...
Try PHP Mailer, with this, you doesn't need to have local SMTP server and also you can also use your validations
<?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 = 'from#example.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->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';
}
This question already has answers here:
PHP : send mail in localhost
(5 answers)
Closed 8 years ago.
I have been trying to send email using php, I am able to send email when I try to run code on Linux but unable to run it on windows 8, I have XAMPP installed on my windows. Here is the code.
<?php
$msg = "Checking Email";
$msg = wordwrap($msg,70);
mail("myemailaddress","My subject",$msg);
?>
I look online and found out that I have to edit php.ini and sendmail.ini file. I am not sure though that this is the right solution. If it is, can anyone please tell me what exactly do I have to edit or change it, because I tried to edit those files but it still not working.
Thanks
If I were you I would try using PHPMailer. I prefer using it over PHPs built in system. I find it less of a hassle.
Here's some of what you can do with it:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.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->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';
}
There is more information on their github page. And there should be no trouble setting it up if you have access to a web server, with email credentials - you would need a host not just XAMPP or LAMP (assuming you intend to get that eventually).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I know HTML & CSS. I am building a website. added a 'contact us' page. I am using free web hosting at this moment which runs a linux server. php mail function is working fine now..
What are the other ways in which an email can be sent automatically through a website on receiving users information?
Thanks in advance..
I suggest PHPMailer , compared with the php function mail() offers many more features
PHPMailer - A full-featured email creation and transfer class for PHP
Class Features
Probably the world's most popular code for sending email from PHP!
Used by many open-source projects: Drupal, 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 and CRAM-MD5 mechanisms over SSL and TLS transports
Native language support
DKIM and S/MIME signing support
Compatible with PHP 5.0 and later
Much more!
Example
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.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->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';
}
PHPMailer
Documentation
you can try PHPMailer library to send e-mails via your own SMTP account.
Sample code:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 encryption, 'ssl' also accepted
$mail->From = 'from#example.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->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 give very little information about your server hosting environment, but the correct answer is to get the php mail to work. There are so many reasons why, that is is impossible to answer without much more information
I have a webapp to send mails and using PHPMailer.
I have generated a contact list which stores the email addresses in database.
Now in my mail function(web page), I do not have any field to enter email address manually, rather I would like to fetch the email id's from table and send mail to all at one click.
Can someone please help me with this, not knowing what to do.
public function actionSendMail(){
$model = YourModelForMails::model()->findall();
foreach($model as $m){
$this->mailfunction($m['emailcolumn']);
}
}
public function mailfunction($email){
sendyourmail to: $email;
}
SoI will not code for you the app but will give really useful hint to make you workout your app.
Use Yii CActiveRecord based Model to fetch User Data (Normally containing Email and Other Details)
Put PHPMailer in vendors Folder and Import using alias path vendor (Hint Yii::import)
Use PHPMailer and Do the Sending. Here I attach PHPMailer's own example
Put Effort to Make them work together. if you cannot, it means you need to put your work aside and go learning!
Code:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh#example.net', 'Josh Adams'); // 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->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;
exit;
}
echo 'Message has been sent';