Sponsored Content
Top Forums Programming Can we send email with C program on Unix? Post 302077783 by jim mcnamara on Monday 26th of June 2006 10:56:49 AM
Old 06-26-2006
sample using mailx:
Code:
#include <stdlib.h>
#include <string.h>

#define cknull(x) if((x)==NULL) {perror(""); exit(EXIT_FAILURE);}
#define cknltz(x) if((x)<0) {perror(""); exit(EXIT_FAILURE);}
#define LIST_LEN 4

/******************************* 
* email_it() -
*  emails the contents of a file
*  parms 
*	char *filename - file to email
*  	email recipients are in the array email_list[][] 
*   which has to be terminated with a zero-length element
********************************/

void email_it(char *filename)                                           
{                                                                       
	char tmp[256]={0x0};                                                
	char fpBuffer[512]={0x0};                                           
	char email_list[LIST_LEN][256]={  {"username@somecompany.com"},             
							   {"username@somecompany.com"},             
							   {"username@anothercompany.com"},         
							   {0x0}};                                  
	int i=0;                                                            
																		
	for(i=0;*email_list[i]>0x0;i++)                                
	{		                                                            
		cknull(strcpy(tmp, email_list[i]));                             
		cknltz(sprintf (fpBuffer,                                       
			"/usr/bin/mailx -s '%s %s' %s < %s",                        
			"Please Review:",                                           
			filename,                                                   
			tmp,                                                        
			filename)); 	                                            
		if(system (fpBuffer)==(-1))                                     
		{                                                               
			perror("email failure");                                    
			exit(EXIT_FAILURE);	                                        
		}                                                               
	}	                                                                
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies

2. UNIX for Dummies Questions & Answers

Send email where # is in the email address - Using Unix

Hi All, How do I send an email using malix where email address contains a #. I have a email address like this : #test@test.com I want to send email like malix -s "TEST" #test@test.com < SOMEFILE I tried \# but doesn't work. Please let me know how we can achieve this? I am in... (1 Reply)
Discussion started by: jingi1234
1 Replies

3. Shell Programming and Scripting

How to Send email in UNIX

Hi, I am new to Shell script programming. Can anyone explain How to Send Mail in UNIX. Can explain in detail please? Thanks in Advance. From Sathish D V. (1 Reply)
Discussion started by: cooolthud
1 Replies

4. Shell Programming and Scripting

send email from unix with attachment

Hello All, This is a common question that I found lot of results in the forums. I am trying to use uuencode to attach a file and send email. I have no issues sending email, but not able to attach any files using sendmail. Is uuencode part of sendmail or does 'uuencode' utility need to be... (1 Reply)
Discussion started by: chiru_h
1 Replies

5. UNIX for Advanced & Expert Users

How to know whether I am able to send email from unix server or not?

How to know whether I am able to send email from unix server or not? I ma trying to send mail to my gmail id using command sendmail -s "Subject" abc@gmail.com Hi How are you? . It showed that You are not allowed to send mail. My question is there any file which contains the list of... (0 Replies)
Discussion started by: siba.s.nayak
0 Replies

6. UNIX for Dummies Questions & Answers

How to send email from HP Unix box

Hi everyone, I am new to HP/UX and I would like to know if there is a way to receive system alerts and logs via email on daily bases. Recently our system crashed due to Hard Disk failure, and since then we are checking ELM every day. I would like to automate that process and have UNIX mail send... (3 Replies)
Discussion started by: eurouno
3 Replies

7. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

8. Shell Programming and Scripting

send email issue in unix

Hi, I am trying to send the contents of a file as email in unix.I am using the below code to do this.But when i get the email i see that SUBJECT and TO is blank. Can you please let me know why it is blank? Also the FROM address comes as admin@myhost. Is there any way i can set FROM to user... (5 Replies)
Discussion started by: vignesh53
5 Replies

9. Shell Programming and Scripting

send email from unix server

HI Guys, I want send email from my script ..and i have tried below command still no luck. ( uuencode $Adm "Test.txt" ) | mailx -s "Test" P***.P****@at***.com mailx -s "test mail" P****.P***@at***.com < $Admin Any other option.... (2 Replies)
Discussion started by: asavaliya
2 Replies

10. Shell Programming and Scripting

Send an email if "No such file or directory" in the shell script program log in EBS concur

Hi All, I have the below code(.sh) and need to send an email. #!/bin/bash cp /u02/xxc_incoming/TEST*.dat /u02/xxc_archive_incoming/AMER7764_ARPP_2/ cat /u02/xxc_incoming/TEST*.dat > /u02/xxc_incoming/XXC_TEST.dat rm /u02/xxc_incoming/TEST*.dat cd $XXC_TOP/bin sqlldr userid=apps/<pwd> ... (12 Replies)
Discussion started by: Mist123
12 Replies
LISTXATTR(2)						     Linux Programmer's Manual						      LISTXATTR(2)

NAME
listxattr, llistxattr, flistxattr - list extended attribute names SYNOPSIS
#include <sys/types.h> #include <sys/xattr.h> ssize_t listxattr(const char *path, char *list, size_t size); ssize_t llistxattr(const char *path, char *list, size_t size); ssize_t flistxattr(int fd, char *list, size_t size); DESCRIPTION
Extended attributes are name:value pairs associated with inodes (files, directories, symbolic links, etc.). They are extensions to the normal attributes which are associated with all inodes in the system (i.e., the stat(2) data). A complete overview of extended attributes concepts can be found in attr(5). listxattr() retrieves the list of extended attribute names associated with the given path in the filesystem. The retrieved list is placed in list, a caller-allocated buffer whose size (in bytes) is specified in the argument size. The list is the set of (null-terminated) names, one after the other. Names of extended attributes to which the calling process does not have access may be omitted from the list. The length of the attribute name list is returned. llistxattr() is identical to listxattr(), except in the case of a symbolic link, where the list of names of extended attributes associated with the link itself is retrieved, not the file that it refers to. flistxattr() is identical to listxattr(), only the open file referred to by fd (as returned by open(2)) is interrogated in place of path. A single extended attribute name is a null-terminated string. The name includes a namespace prefix; there may be several, disjoint names- paces associated with an individual inode. If size is specified as zero, these calls return the current size of the list of extended attribute names (and leave list unchanged). This can be used to determine the size of the buffer that should be supplied in a subsequent call. (But, bear in mind that there is a possibil- ity that the set of extended attributes may change between the two calls, so that it is still necessary to check the return status from the second call.) Example The list of names is returned as an unordered array of null-terminated character strings (attribute names are separated by null bytes ('')), like this: user.name1system.name1user.name2 Filesystems that implement POSIX ACLs using extended attributes might return a list like this: system.posix_acl_accesssystem.posix_acl_default RETURN VALUE
On success, a nonnegative number is returned indicating the size of the extended attribute name list. On failure, -1 is returned and errno is set appropriately. ERRORS
ENOTSUP Extended attributes are not supported by the filesystem, or are disabled. ERANGE The size of the list buffer is too small to hold the result. In addition, the errors documented in stat(2) can also occur. VERSIONS
These system calls have been available on Linux since kernel 2.4; glibc support is provided since version 2.3. CONFORMING TO
These system calls are Linux-specific. EXAMPLE
The following program demonstrates the usage of listxattr() and getxattr(2). For the file whose pathname is provided as a command-line argument, it lists all extended file attributes and their values. To keep the code simple, the program assumes that attribute keys and values are constant during the execution of the program. A production program should expect and handle changes during execution of the program. For example, the number of bytes required for attribute keys might increase between the two calls to listxattr(). An application could handle this possibility using a loop that retries the call (per- haps up to a predetermined maximum number of attempts) with a larger buffer each time it fails with the error ERANGE. Calls to getxattr(2) could be handled similarly. The following output was recorded by first creating a file, setting some extended file attributes, and then listing the attributes with the example program. Example output $ touch /tmp/foo $ setfattr -n user.fred -v chocolate /tmp/foo $ setfattr -n user.frieda -v bar /tmp/foo $ setfattr -n user.empty /tmp/foo $ ./listxattr /tmp/foo user.fred: chocolate user.frieda: bar user.empty: <no value> Program source (listxattr.c) #include <malloc.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/xattr.h> int main(int argc, char *argv[]) { ssize_t buflen, keylen, vallen; char *buf, *key, *val; if (argc != 2) { fprintf(stderr, "Usage: %s path ", argv[0]); exit(EXIT_FAILURE); } /* * Determine the length of the buffer needed. */ buflen = listxattr(argv[1], NULL, 0); if (buflen == -1) { perror("listxattr"); exit(EXIT_FAILURE); } if (buflen == 0) { printf("%s has no attributes. ", argv[1]); exit(EXIT_SUCCESS); } /* * Allocate the buffer. */ buf = malloc(buflen); if (buf == NULL) { perror("malloc"); exit(EXIT_FAILURE); } /* * Copy the list of attribute keys to the buffer. */ buflen = listxattr(argv[1], buf, buflen); if (buflen == -1) { perror("listxattr"); exit(EXIT_FAILURE); } /* * Loop over the list of zero terminated strings with the * attribute keys. Use the remaining buffer length to determine * the end of the list. */ key = buf; while (buflen > 0) { /* * Output attribute key. */ printf("%s: ", key); /* * Determine length of the value. */ vallen = getxattr(argv[1], key, NULL, 0); if (vallen == -1) perror("getxattr"); if (vallen > 0) { /* * Allocate value buffer. * One extra byte is needed to append 0x00. */ val = malloc(vallen + 1); if (val == NULL) { perror("malloc"); exit(EXIT_FAILURE); } /* * Copy value to buffer. */ vallen = getxattr(argv[1], key, val, vallen); if (vallen == -1) perror("getxattr"); else { /* * Output attribute value. */ val[vallen] = 0; printf("%s", val); } free(val); } else if (vallen == 0) printf("<no value>"); printf(" "); /* * Forward to next attribute key. */ keylen = strlen(key) + 1; buflen -= keylen; key += keylen; } free(buf); exit(EXIT_SUCCESS); } SEE ALSO
getfattr(1), setfattr(1), getxattr(2), open(2), removexattr(2), setxattr(2), stat(2), attr(5), symlink(7) Linux 2015-02-21 LISTXATTR(2)
All times are GMT -4. The time now is 10:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy