Mail function php not sending

PHP mail() function not sending on new server

Having some issues with a PHP script that sends emails (code below). Basically, it populates a vCard file with contact information stored in an sql db and attaches it to an email using the php mail() function. I had this working perfectly on a shared hosting server a few days ago. but I recently migrated everything over to a VPS and it magically stopped working. Mail() continues to return true on send, but the actual email never arrives in my inbox.

//sendemail: emails a vCard when passed an email address and name function sendemail($address, $scanreduser) < include('../dbconnect.php'); $info = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE usernum='". $usernum ."' LIMIT 1 ")); $vcard_content = "BEGIN:VCARD\r"; $vcard_content .= "VERSION:3.0\r"; $vcard_content .= "N:".$info[lname].";". $info[fname] .";;;\r"; $vcard_content .= "FN:".$info[fname]." ". $info[lname] ."\r"; $vcard_content .= "ORG:".$info[company].";\r"; $vcard_content .= "TITLE:".$info[title]."\r"; $vcard_content .= "EMAIL;type=INTERNET;type=WORK;type=pref:".$info[email]."\r"; $vcard_content .= "TEL;type=WORK;type=pref:".$info[phone]."\r"; $vcard_content .= "item2.URL;type=pref:".$info[website]."\r"; $vcard_content .= "item2.X-ABLabel:_$!!\$\_\r"; $vcard_content .= "X-ABShowAs:COMPANY\r"; $vcard_content .= "END:VCARD"; $email_subject = "Your vCard from " . $info[fname] . " " . $info[lname]; $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = $info[fname] ."_". $info[lname] .".vcf"; $headers = "From: Ben@scanred.com"; $today = date("l, F j, Y, g:i a"); $message = "
Simply open the attached vCard file to view/download the information
"; $message .= $today." PST

"; $message .= $info[name]."
"; $data = $vcard_content; $data = chunk_split(base64_encode($data)); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_xx"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"\""; $message .= "This is a multi-part message in MIME format.\n\n" . "--\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $message .= "--\n" . "Content-Type: ;\n" . " name=\"\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "----\n"; echo @mail($address, $email_subject, $message, $headers); echo $address; >

Does the new server have a properly configured SMTP server (sendmail, postfix, qmail, etc.)? If so, check your maillog in /var/log/mail* . Also, if you lose the @ before the email you might also get a useful error message to work with .

Источник

PHP mail function not working on Centos server

I am using centos Server and have to send the mail to the user so i copied running code of mine from one server and used it in here, but it is not sending mails. Code is :

 $to = $email; //writing mail to the user $subject = "Hii"; $message = " 
Hello ".$email.",
Some Text
Some Text
Some Text
Some Text
" ; $from = "example@domain.com"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'From: Team ' . "\r\n"; if(mail($to,$subject,$message,$headers)) < echo "0";// mail sent Successfully. >else

It always print 1. Same code running fine on other project. Please guide me what i can do to enable it here too? Any help will be highly appreciated!

yum install sendmail should def help, cause if you havent installed it, chances are its not there in the system.

Читайте также:  Model fitting in python

In case you don’t want to install sendmail at once (as most answers here for some reason suggest), you can try «/etc/init.d/sendmail status» or «/etc/init.d/postfix status» and «cat /var/log/maillog»

7 Answers 7

After installing sendmail* and running the following commands:

[root@sendmail ~]# yum install sendmail* [root@sendmail mail]# yum install dovecot [root@sendmail mail]# cd /etc/mail/ [root@sendmail mail]# vi local-host-names # local-host-names - include all aliases for your machine here. example.com [root@sendmail mail]# vi /etc/dovecot.conf protocols = imap pop3 //uncomment [root@sendmail mail]# m4 sendmail.mc > sendmail.cf [root@sendmail mail]# make [root@sendmail mail]# /etc/init.d/sendmail start [root@sendmail mail]# /etc/init.d/saslauthd start [root@sendmail mail]# /etc/init.d/dovecot start [root@sendmail mail]# chkconfig sendmail on [root@sendmail mail]# chkconfig dovecot on [root@sendmail mail]# chkconfig saslauthd on 

I still had the same issue. I checked my /var/log/maillog and saw an error:

My unqualified host name (domain) unknown; sleeping for retry 

After more searching, I changed /etc/hosts from:

127.0.0.1 localhost localhost.localdomain domain ip.ip.ip.ip domain localhost 
 127.0.0.1 localhost.localdomain localhost domain ip.ip.ip.ip localhost domain 

and now the mail function is now working fine.

I used these instructions to set up sendmail for CentOS in a Docker container. I did however only have to to do yum install sendmail to use mail(). Configuring the hosts file as above proved critical in order to make the mail function send email in a timely fashion. Thank you!

I know this has been answered but I had a similiar problem. In case anyone else.

/var/log/maillog showed me a Postfix permission issue.

sendmail: fatal: chdir /var/spool/postfix: Permission denied 

Tracking down error I found solution to be SELinux policy on CentOS (I’m using version 6).

Quick answer: setsebool httpd_can_sendmail 1

You can use -P to make the change permanent; I just needed for password reset e-mail so not needed for my case.

EDIT: I would have commented but I don’t have enough reputation yet.

I had the same problem. I have a development server at home and prod server at external server house and incoming mails go to other server. PHP:s mail() works nicely in server house, but not at home.

I tested a bit and got it to work at home in the same manner as in server house. The difference between methods in server house and home is the configuration of sendmail. Server house I had only to install sendmail and it was fine, but at home I had to install also sendmail-cf and use it to add outgoing mail server address.

Let’s assume you have Centos, Apache and PHP at home server and you want to send emails using PHP:s mail() function.

1) Set hostname on home server into two places: /etc/sysconfig/network and /proc/sys/kernel/hostname this way:

# nano /etc/sysconfig/network NETWORKING=yes HOSTNAME=mydns157.dlinkddns.com # nano /proc/sys/kernel/hostname HOSTNAME=mydns157.dlinkddns.com

2) Install sendmail and sendmail-cf:

# yum install sendmail sendmail-cf

3) Add the following row into /etc/mail/sendmail.mc, where you have your ISP:s outgoing mail server:

define(`SMART_HOST',`mail.myisp.com')dnl

5) Restart sendmail and apache:

# service sendmail restart # service httpd restart

6) Boot to update hostname:

Читайте также:  Выбрать все файлы папки php

That’s it. Now the following works:

# php -r'mail("user@somedomain.com", "Subject", "Body", null, "-fme@mydomain.com");'
# php -r'mail("user@somedomain.com", "Subject", "Body");'

in which case the sender’s name become automatically user@hostname, eg. root@mydns157.dlinkddns.com.

Some notes of hostname

The selection of hostname is critical. The default in Centos6 is localhost.localdomain , but mail() is not working with it if you skip your own sender address (eg. ‘-fme@mydomain.com’ ) when calling mail(). If you are sure, that you always call mail() using your real address as sender address, the hostname can be whatever, but if you have existing calls to mail() that lacks the sender address (I have hundreds of this like calls for testing purposes), then you must have a real domain as a hostname, because your server’s hostname is used as a sender-address-domain in these cases. Real in the sense that domain must have at least DNS A-record (for some reason my ISP doesn’t require NS-record for sender address, only A-record, but test and check with your ISP). The downside of using non-email-domain as a sender address is that replys and delivery notifications will go to bit’s heaven, but if you drop sender address in your code, it usually means that you test and debug something and don’t need reply functionality. The domain can be eg. the one you get from dynamic dns server eg. mydns157.dlinkddns.com , which can point to your home router (but not have to). You can get DNS record in PHP using dns_get_record(«mydns157.dlinkddns.com») , which returns an array like this:

[host] => mydns157.dlinkddns.com [type] => A [ip] => 92.152.214.137 [class] => IN [ttl] => 7

If type in above DNS-record is NS , then the domain acts as an email domain, which is OK for hostname of own server, but the effect is a little different. If you set as hostname an existing email domain eg. myexistingemaildomain.com , and send a message to me@myexistingemaildomain.com eg for debugging purposes, sendmail thinks that the message has to be delivered to mail-folder of user me on this server. If me user doesn’t exist, the sending fails and if the user exists, then the message goes to /var/mail/me . This can be what you want, but (like me) you may want that all messages are delivered outside instead of server folders.

Your hostname (in DNS record) doesn’t need to point to your server’s actual external IP to make mail() work in lack-of-sender-address cases, but there is no harm of it. The main thing is that hostname has a valid A-record and that the domain belongs to you. If the domain doesn’t belong to you, then there may born a security hole. If you set as hostname some existing email domain eg. microsoft.com (for whatever reason) and send a message to someone without adding your own sender address when calling mail() (eg. ‘-fme@mydomain.com’ ), the sender address will be automatically you@microsoft.com . If you are logged as root, the sender address will be root@microsoft.com . Replys and notifications of failed deliveries go then to root@microsoft.com and this may not be your intention.

Читайте также:  Kata academy by java mentor

Источник

Mail function not working — PHP

I am new to PHP and just started learning it. I am trying to send a test mail to my gmail account using the mail() function. Here is my code:

$message = "Hello PHP!"; mail("mygmailaccount@gmail.com", "PHP Test", $message); 

But its not working. This is what the error looks like:

Click here to view it in a webpage.
I have seen lots of similar questions and I have also tried all the solutions mentioned on SO.
I have made changes to my php.ini file as shown below.

[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = smtp.gmail.com ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = mygmailaccount@gmail.com 

@PoomrokcThe3years Thanks for your quick reply. I googled it. But can I not make the changes in the PHP.ini file and use the mail function?

2 Answers 2

This is the official PHPMailer.

all you have to do is upload all the files and folders to a folder with the mailing php file(the file which have the code shown by you). Then look at the example folder or look at the example folders if you need smtp.You should have figured it out , looks like you got good programming skill.If you have problem , comment to informed me.

PS. mail() function sometimes not reliable. I face that problem often until I use phpmailer for my own system.

Folder containg phpmailer

EDIT: Put it altogether like this. send.php is the file I’m going to write the followling code.

 IsSMTP(); // telling the class to use SMTP $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "*hidden*"; // GMAIL username Input your own $mail->Password = "*hidden"; // GMAIL password Input your own > //Typical mail data $mail->AddAddress($sendto,$sendto); $mail->SetFrom($sendfrom,$sendfrom); $mail->Subject =$topic; $mail->Body =$passage; try< $mail->Send(); echo "Success! This email has been sent to ".$sendto. " in the name of ".$sendfrom; > catch(Exception $e) < //Something went bad echo "Unable to process your request now, please try again later"; >?> 

Change the mailsender,mailreciever and contents of the mail. Also , input your gmail username and password. After that, run the php, wait for your email to come. I’ve just test it 8 minutes ago and it worked.

Источник

Оцените статью